Summary of value types and reference types

Source: Internet
Author: User

The following are some of my thoughts on the Value Type and reference type. There are certainly some mistakes. You are welcome to criticize and correct them to help you make progress.

In C #, the value type and reference type are two very important concepts. We will try to elaborate in detail below. The viewpoint may not be completely correct. Please criticize and correct it.

1. Memory is divided into heap and stack. Value-type data is stored in the stack, and reference-type data is stored in the heap.

2. Int numb = 10. 10 in the Code is a value type data, and numb is just a variable pointing to 10. 10 of them are stored in the stack, and the variable Numb is also stored in the stack.

3. Person P = new person (). The object created by new person () in the Code is stored in the heap, and variable P is stored in the stack.

4. All variables at points 2nd and 3rd are stored in the stack, regardless of the value type and reference type.

5. value types include enumeration, structures, and simple types such as int and datetime. reference types include classes, interfaces, and delegation.

6. Copy the value passed by the value type. The reference type transfers the value reference, that is, the address of the data in the memory. This is the core concept. The example shows:

Class program {static void main (string [] ARGs) {person yaohongbo = new person () {name = "Yao Hongbo", age = 29}; person lilei = new person () {name = "Li Lei", age = 22}; int age = yaohongbo. age; yaohongbo. age = 27; person zhangsan = lilei; lilei. name = "Li Lei"; console. writeline ("yaohongbo age:" + yaohongbo. age); console. writeline ("Age:" + age); console. writeline ("Name of lilei:" + lilei. name); console. writeline ("Name of zhangsan:" + zhangsan. name); console. readkey () ;}} class person {public string name {Get; Set ;}// reference type public int age {Get; Set ;}// Value Type}

 

The output result is as follows:

Code Description: yaohongbo. age is 'int' type and 'value' type, so the code int age = yaohongbo. age, which is to convert yaohongbo. the value stored in the age variable is copied, and the variable Age is used to store the copy. age does not have any relationship, so we can see the output above.

Look at the code again:

Person zhangsan = lilei; lilei. Name = "Li Lei ";

 

Person is a user-defined class and a reference type. The reference type is referenced. The data pointed to by the variable zhangsan and the variable lilei are the same memory address, that is, the same object, instead of copying the data pointed to by the variable lilei to the variable zhangsan as the value type. Therefore, no matter the attribute value of zhangsan Korean lilei is changed, the other will change.

Finally, the above Code is described in a figure.

 

 

Summary of value types and reference types

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.