Examples of c value types and reference types used

  • 2020-06-19 11:38:30
  • OfStack

In the first job interview, we often encounter questions about value types and reference types. How you answer directly affects your impression on others. Your poor answer indicates that you do not have a deep understanding of C#. CLR supports two types: reference types and value types. While most types in FCL are reference types, value types are the most commonly used in programs. Reference types are always allocated from the managed heap, and the new operation of C# returns the memory address of the object -- that is, the memory address of the data pointing to the object. To improve the performance of simple, commonly used types, CLR provides a lightweight type called "value types". Instance 1 of the value type is normally allocated on the thread stack. In a variable that represents an instance of a value type, there is no pointer to the instance. Instead, variables contain fields for the instance itself. Instances of value types are not controlled by the garbage collector. Thus, the use of value types relieves stress in the managed heap and reduces the number of garbage collections an application needs to do during its life cycle. .net framework SDK documents clearly indicate which types are reference types and which are value types. Any type called a "class" is a reference type, for example, the System.Exception class, System.IO.FileStream class, and System.Random class are reference types. Instead, the document calls all value types structure or enumeration. For example, the System.In32 structure, System.Boolean structure, System.Decimal structure, System.DayOfWeek enumeration, and System.Drawing.FontStyle enumeration are value types. All structures are directly derived classes of the abstract type ES30en.ValueType. System. ValueType itself is derived from System. Object, and all value types must be derived from System. ValueType. All enumerations are derived from the System.Enum abstract type, which in turn is derived from System.ValueType.

The following code demonstrates the reference type and value type.


// Reference type (due to the use of ' Class' ) 
class SomeRef{public Int32 x;}

// Value type (due to the use of ' Struct' ) 
struct SomeVal{public Int32 x;}

static void ValueTypeDemo(){
SomeRef r1 = new SomeRef();   // Distribute on the heap 
SomeVal v1 = new SomeVal(); // Allocation on the stack 
r1.x = 5; // Pointer to withdraw 
v1.x = 5;   // Modify on the stack 
Console.WriteLine(r1.x);   // According to" 5 " 
Console.WriteLine(v1.x);  // Also show" 5 " 

SomeRef r2 = r1; // Copy only references (Pointers) 
SomeVal v2 = v1;   // Allocate and copy members on the stack 
r1.x = 8;  //r1.x and r2.x Will change 
v1.x = 9; //v1.x It will change, however v2.x The same 

Console.WriteLine(r1.x);   // According to" 8 " 
Console.WriteLine(r2.x);   // According to" 8 " 
Console.WriteLine(v1.x);   // According to" 9 " 
Console.WriteLine(v2.x); // According to" 5 " 
}


Related articles: