This article mainly for you to introduce in detail the C # value types and reference types of relevant data, with a certain reference value, interested in small partners can refer to
In C #, value types and reference types are two very important concepts that must determine the behavior of a type instance when designing a type. If you do not understand the differences between reference types and value types when writing code, you will introduce unnecessary exceptions to your code. Many people simply do not understand these two concepts in order to encounter a lot of problems in the programming process, here bloggers talk about value types and reference types of understanding.
First, conceptually, a value type stores its value directly, whereas a reference type stores a reference to its value. Thus, these two types are stored in different places in memory.
Second, from the memory space, the value type is manipulated in the stack, and the reference type allocates the storage unit in the heap.
Stack in the compile time allocated memory space, in the code has a clear definition of the stack, and the heap is a program run dynamically allocated memory space, you can dynamically allocate the size of memory according to the program's running situation. Therefore, the value type always consumes a predefined number of bytes in memory. A variable of reference type allocates a memory space in the stack, which contains a reference to another memory location, which is an address in the managed heap, where the actual value of the variable is stored.
That is, the value type is equivalent to cash, to use directly, and the type equivalent to the Passbook, to be used first to the bank to take.
But the value type allocates memory on the stack, and the reference type allocates memory on the managed heap, just a general statement. It is described in detail below.
(1) For an instance of a value type, if it is a local variable in the method, it is created on the thread stack, and if the instance is a member of a type, it is stored on the managed heap as part of the type member, along with other type fields.
Each value type has an implicit default constructor to initialize the default value for that type. For example:
int i = new int ();
Equivalent to:
Int32 i = new Int32 ();
Equivalent to:
int i = 0;
Equivalent to:
Int32 i = 0;
When you use the new operator, a specific type of default constructor is called and the variable is assigned the default value. In the example above, the default constructor assigns the value 0 to I.
Description: All value types in C # are implicitly derived from System.ValueType, whereas System.ValueType is derived directly from System.Object. That is, System.ValueType itself is a class type, not a value type. The key is that ValueType overrides the Equals method to compare the value type against the value of the instance, rather than the reference address.
(2) An instance of a reference type is created on the managed heap.
Here is a piece of code to explain in detail the difference between a value type and a reference type
Namespace Test {class Program {static void Main (string[] args) {//Call Referenceandva The demonstration method in the Lue class is referenceandvalue.demonstration (); Console.ReadLine (); }} public class stamp//defines a class {public string Name {get; set;} Defines a reference type public int an age {get; set;} Define value type} public static class Referenceandvalue//define a static classes {public static void demonstration () Define a static method {stamp stamp_1 = new Stamp {Name = "Premiere", age = 25};//Instantiate Stamp stamp_2 = new Stamp {Name = "Again", age = 47}; Instantiate int age = Stamp_1.age; Gets the value of type age value Stamp_1.age = 22; Modify the value of a value type stamp guru = stamp_2; Gets the value in stamp_2 stamp_2.name = "Again Amend"; Modify the reference Name Value Console.WriteLine ("Stamp_1 ' age:{0}", Stamp_1.age); Displays the Age value Console.WriteLine ("Age ' value:{0}" in Stamp_1, age); Displays the Age value Console.WriteLine ("Stamp_2 ' name:{0}", Stamp_2.name); Displays the name value in Stamp_2 Console.WriteLine ("Guru ' name:{0}", Guru. Name); Display the name value in Guru}}}
By running the above program we can see that when the value of stamp_1.age is changed, age does not change, but changes the Anders. After the value of name, Guru. Name is changed, which is the difference between a value type and a reference type. When declaring an age value type variable, assign the value of Stamp_1.age to it, at which point the compiler allocates a space on the stack and then fills in the Stamp_1.age value without any association, just like copying a file on a computer, simply putting the stamp_1. The value of age is copied to age. The reference type is different, when declaring guru, assign stamp_2 to it, as mentioned earlier, the reference type contains only the reference to the data region address on the heap, in fact, the stamp_2 reference is also assigned to guru, so they point to the same piece of memory area. Since it is pointing to the same area, no matter who modifies, the value of the other will change, just like the credit card with the family card, with the family card to take the money, and the associated credit card account will also follow the change.