". Net" on value types and reference types in C #

Source: Internet
Author: User

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:

Equivalent to:

Equivalent to:

Equivalent to:

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

 1 namespace Test 2 {3 class program 4 {5 static void Main (string[] ar GS) 6 {7//Call demonstration method in Referenceandvalue Class 8 Referenceandvalu   E.demonstration ();  9 Console.ReadLine ();                Ten} each} public class stamp//define a class 13 {14       public string Name {get; set;}       Defines a reference type of a public int, age {get; set;}                Defining a value type of 18} public static class Referenceandvalue//define a static public static void Demonstration ()//define a static method {Stamp Stamp_1 = new S Tamp {Name = "Premiere", age = 25}; Instantiation of Stamp stamp_2 = new Stamp {Name = "Again", age = 47};         Instantiation of A ()-int = Stamp_1.age; Get value type the value of age STamp_1.age = 22;          To 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 of Console.WriteLine ("Stamp_1 ' age:{0}", Stamp_1.age); Displays the age value in Stamp_1 Console.WriteLine ("Age ' value:{0}", age); Displays the age value of Console.WriteLine ("Stamp_2 ' name:{0}", Stamp_2.name); Displays the name value in Stamp_2 of Console.WriteLine ("Guru ' name:{0}", Guru. Name);  Display the name value in Guru 31} 32} 33}

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.

". Net" on value types and reference types in C #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.