Translated from Eric Lippert's Blog, original: http://blogs.msdn.com/ericlippert/archive/2009/08/27/what-s-the-difference-between-fixed-and-fixed.aspx
One day I received an email starting with this:
I have a question about the fixed size buffer of C:
Unsafe struct FixedBuffer {public fixed int buffer [100];}
Now the buffer is declared as fixed, and it cannot be moved...
My heart is broken when I see this problem. If the detailed language design is easy to cause misunderstanding, the above situation is only one of the most unfortunate moments.
When using pointer algorithms for hosted objects in unsafe code, make sure that the garbage collector does not remove the memory you need. When you are using a pointer to operate an object, if another thread recycles resources, the pointer will be completely confused. Therefore, C # divides variables into "fixed" and "variable ". If you want to apply the pointer to a movable object, you can use the "fixed" keyword to declare that "this local variable cannot be moved by the garbage collector ". When the recycle action occurs, the garbage collector needs to view all local variables for those ongoing calls (the variables used need to be retained). If the collector sees a variable marked as "fixed ", it will not move such a variable, even if it will cause managed heap fragments (it is important to make such a variable as little time as possible ). So typically, we use "fixed" to represent being fixed somewhere.
However, the "fixed" in this email does not mean this. It means that the buffer size in this question is fixed to accommodate 100 int variables. In essence, this is the same as creating 100 int type members in the structure.
Obviously, we often use the same keywords to represent the same concept. For example, in C #, we often use the keyword "internal" in different ways, but all "internal" indicates the same meaning. It is only used to indicate that "some entity access permissions are unrestricted in the code of the same assembly ".
In some cases, we also use the same keywords to represent completely different concepts. This depends on the context used by the user to determine what it means. For example:
Var results = from c in customers where c. City = "London" select c;
And
Class C <T> where T: IComparable <T>
Obviously, "where" is used in two completely different ways: Create a filter clause in a query statement, and declare the constraint type of a generic parameter.
If a keyword is used for two different meanings, but the difference is very subtle, like the example we mentioned above, it will make people encounter difficulties. The user's email will continue to ask a lot of questions, but these questions are based on an incorrect assumption-a fixed size buffer will be automatically fixed to a location in the memory.
This can only be said that this is a bad mix of terms: "fixed size" and "fixed location" both use "fixed", but the Use method is different, which is a headache. However, the association between them is more complex than this: only when a container with a fixed buffer size is fixed in a certain memory zone can you securely access the data stored in the buffer zone. In this issue, these two concepts have strong relevance, but they are not exactly the same.
On the one hand, to reduce confusion, we can use two different keywords, such as "pinned" and "fixed ". On the other hand, all "fixed" usage is only available in non-secure code. For all the features of insecure codes, a key premise assumption is: if you want to use non-secure code in C, then you must have been a programming expert who fully understands the memory management in CLR. This is why we asked you to mark "unsafe" in the Code; it indicates that you have disabled the security system and know what you are doing.
Important keywords that can be used in C #: fixed, into, partial, out, in, new, delegate, where, using, class, struct, true, false, base, this, event, return, and void both have at least two different meanings. Most of them are clear in specific contexts, but at least the first three -- fixed, into, and partial -- have caused a lot of trouble, I have received a lot of questions about their usage differences. Next I will take a look at "into" and "partial ".