Basic knowledge that is often misunderstood C,
- Misunderstanding of value type and reference type (the reference type is stored on the stack, and the value type is stored on the stack)
When learning C # basic space, I can't escape the value type and reference type. Many new users, including my previous understanding of it, just stay in"The reference type is stored on the stack,The value type is stored on the stack.".
This misunderstanding is mainly attributed to the fact that we have no brains at all. The first sentence is correct, and reference instances are always created on the stack. but there is a problem with the last sentence. suppose a class has an int type instance variable.
public class User{ public int Age { get; set; }}
The value of this variable in this User class is always in the same way as other data in the object, that is, in the stack. in fact, only local variables and method parameters are on the stack, but some local variables are not stored in special cases.
On the stack, such as the anonymous function closure, these features are not suitable for beginners.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
This issue is confusing when JavaScript programmers learn C. although C # is not a completely strong-type language (C #4.0 or above allows you to use dynamic types), C #3.0 or below is basically a strong-type language.
Before explaining the var keyword, we need to know that C #2.0 has modified the CLR, but basically no major changes have been made in subsequent versions, this means that many of the features added later are the help of the compiler,
For example, C #3.0 introducesVarType. To better demonstrate that Var is not a dynamic type, I will introduce an instance.
static void Main(string[] args){ var str = "Hello, world."; str = 10;}
The above Code cannot be compiled, and the compiler will tell you"The type "int" cannot be implicitly converted to "string ".".
Because the str type is String, the data type is determined during compilation, so you cannot use the following code
Static void Main (string [] args) {var obj = null; // declare a null pointer var obj ;}
This also proves that it does determine the type at compilation, but the compiler helps you to change the magic. Because the compiler deduced at compilation, it does not know what this is, in this way, its magic will fail.
(Subsequent supplements ......)