C # I understand the value type and reference type,

Source: Internet
Author: User

C # I understand the value type and reference type,

For a long time, both the value type and the reference type are just a fuzzy concept, so I have a thorough understanding of them when I have time.

First, let's talk about the value type. On msdn, we will introduce the value type.

This means that the value type directly contains the value.

The variable reference location is the actual storage location of the value in the memory. Therefore, if you assign the value of an original variable to another variable, A memory copy of the original variable value is created at the position of the new variable. It is impossible for two variables to reference the same memory location drop (unless the parameter is out or ref ). Changing the value of the original variable does not affect the value of another variable, because each variable has its own storage location. As shown in (do not mind if you paint it yourself)

After turning over c # Essence Theory, I vividly and vividly described the value type, which is interesting and understandable at a glance.

The book says: A value type variable is like a piece of paper filled with numbers. To change a number, you can erase it and write different numbers. If there is another piece of paper, you can copy the number on the first one. However, since then, these two sheets of paper are independent. Erasing and replacing numbers on one piece of paper does not affect the numbers on the other piece of paper.

What is the value type? First, let's take a look at the official msdn introduction.

The value of the reference type is a reference to an object instance.

Variables of the reference type store references (usually memory addresses), and then locate the data of the object instance by location. Because, when accessing data, you need to read the reference from the variable, and then obtain the resource based on the reference, so as to reach the memory location that actually contains the instance data.

Variables of the reference type are associated with two storage locations:Storage location directly associated with the variableAndMemory location referenced by the value stored in the Variable.

Let's look at the picture.

 

Like the value reference type, I read the following book, which describes that variables of the reference type are also like a piece of paper with something always written above. For example, assume that the home address "123 Sesame Street, New York City" is written on a piece of paper. This paper is a variable and the address is a reference to a building. Neither the paper nor the address written above is the building itself, and there is no relation between where the paper is and where the building pointed to by the content on the paper. Copy this reference on another piece of paper. The content of both sheets points to the same building. In the future, the building will all turn green, because the reference points to the same thing.

 

End: value-type variables directly store instance data. For the reference type, you need to locate the actual data storage location based on the reference. Copying a value type copies all the data, which may be large. copying a reference type is only a copy reference, which is very small. Therefore, copying music is more efficient.

 

I feel more impressed when I write a blog. If you understand something wrong, please correct me.

This article has been referred:Mdcn official document and c # Essence (version 4th)

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.