value types and reference types
Most basic data types in C # are value types, except that the string type is a reference type;
A class is a reference type, and a struct is a value type;
null values and nullable types
A null value is useful when initializing a reference type, but null itself is a reference and cannot be assigned to a value type, in C # The following statement is illegal: int i=null;//not legal
However, with a modifier defined by C #, you can declare a variable as aNullable (Nullable)The value type. A nullable value type is similar in behavior to a normal value type, but a null value can be assigned to it. We use a question mark(?)To make a value type nullable, as follows:
Int? i=null;//Legal
To determine whether a nullable variable is empty, you can determine the same way as a reference type:
if (i==null)
An expression with the appropriate value type can be assigned directly to a nullable variable. Otherwise, the null variable can not assign values to ordinary value type variables.
Understanding nullable types of properties
Nullable types reveal two properties: The HasValue property indicates whether a nullable type contains a true value or null, and if it contains a true value, it can be obtained using the Value property.
Int? I=null;
....
if (!i.hasvalue)
i=99;
Else
Console.WriteLine (i.value);
If I do not contain a real value, that is NULL, assign 99 to it, otherwise, print its value.
Note: The Value property of a nullable type is read-only and cannot be modified, and the values of nullable variables are modified, using normal assignment statements.
The memory organization of the computer--heap and stack
when a method is invoked, the memory required by its arguments and local variables is always fetched from the stack;
Using the new keyword to create an object, the memory required to construct the object is always fetched from the heap;
Stacks are like a series of increasingly high boxes, accessed sequentially, while the heap is like a pile of boxes scattered around the room, and when the object is created, the "runtime" finds an empty box and assigns it to the object;
Reference types are placed on the heap, and value types are placed on the stack.