C # series value type and reference type

Source: Internet
Author: User

Preface I have been thinking about what I will discuss in this chapter over the past few days. In the previous chapter, we will discuss the reference types involved in string, so we will discuss the value types and reference types in this chapter. The value type and reference type are different from the value passing method. Some people think that the value type exists on the stack, which is not necessarily true. For more information, see the following. Some may say yes .. There is a garbage collection mechanism to clean up the memory .. You don't have to worry about it --!..... In order to write the code and avoid interview questions .. Let's leave it difficult to discuss ..... The value type and stack are stored, instead of the value data type of the object member, as well as local variables and parameters. How does it work? First, you must know that the data on the stack is filled from the high memory location to the low memory location, and the variable address will not be repeated. Suppose there is the following code: From the above we can see that code block B is nested in code block. Because of the execution sequence of the code from top to bottom, variable a is first written into the stack than variable B. But code block B ends earlier than code a. After the code is out of scope, the variable is released, therefore, variable B is released first than variable. It can be seen that the flexibility is not high. If you want to live for a long time? At this time, the role of the heap is reflected. The reference type is different from the heap type. It stores the object member type and is filled from the low memory location to the high memory location. When the value type variable is a type member, it is stored in the heap with the object. Let's talk about how it works. There is such a piece of code. Copy the code class Program {static void Main (string [] args) {Human h; h = new Human () ;}} to copy the code. First, a space is allocated on the stack, store reference h. It is just a reference, not an object. To instantiate the object in the second sentence, the new operator is used to request the allocation of storage space. CLR will search for enough locations on the stack and assign them to the object, then, new will return the address on the heap where it is located to the reference. Therefore, the stack stores the address of the reference pointing to the object on the stack. To sum up, the use of value types reduces the heap pressure and the number of garbage collection times. The reference type makes up for the lack of lifecycle and increases flexibility. The default value is assigned when a value type is created. For example, the default value of int is 0. The default value for creating a variable of the reference type is null. If no reference type of the object is available, an exception NullReferenceException is reported. A value-type variable is assigned a value in the copy mode and performs a copy by field. The reference type assigns the address of the object on the stack to the new variable for reference. The following code fully demonstrates the difference between the reference type and the Value Type: (using the example of Clr C #) copy the code struct Struct // value type {public int x ;} class Class // reference type {public int x;} class Program {static void Main (string [] args) {// code A Struct s1 = new Struct (); // assigned to the stack Class c1 = new Class (); // assigned to the stack s1.x = 1; c1.x = 1; Console. writeLine (s1.x); // output 1 Console. writeLine (c1.x); // output 1 // code B Struct s2 = s1; // copy the stack members to s2 Class c2 = c1; // copy and reference to c2 s2.x = 2; // value type, s1.x unchanged, s2. X changes c2.x = 2; // The reference type. Both c1.x and c2.x change the Console. writeLine (s1.x); // 1 Value Type Console. writeLine (s2.x); // 2 value type Console. writeLine (c1.x); // 2 reference type Console. writeLine (c2.x); // 2 reference type} copy code c1 to copy the address to c2, that is, c1 and c2 point to the same object, therefore, one of c1 and c2 is modified, and the other is also affected. S1 copies members to s2. Although s1 and s2 store the same value, their memory addresses are different and their own values are stored, therefore, s1 and s2 are actually modified and will not affect the other one. The end is almost the same here. In fact, when it comes to the reference type and value type, it also involves packing and unpacking. The younger brother has not yet had a deep understanding of the block and will discuss it with you later. Catch a cold .. Rest... It seems to be worse today than a week .. The company's flu is so bad .. I hope you will pay attention to your health .. =. = .....

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.