From the memory perspective, the difference between the C # value type and the reference type

Source: Internet
Author: User

 

1. Value-type data is stored in the memory stack; reference-type data is stored in the memory heap, while memory units only store
Address.

2. Fast value-type access and slow reference-type access.

3. The value type indicates the actual data, and the reference type indicates the pointer or reference to the data stored in the memory heap.

4. The value type inherits from system. valuetype, and the reference type inherits from system. object.

5. The stack memory allocation is automatically released, and the stack will be released by GC in. net.

6. variables of the value type directly store the actual data, while variables of the reference type store the data address, that is, object reference.
7. The Value Type Variable directly stores the value of the variable in the stack, and the reference type variable stores the actual data address in the stack, while the actual

The data is stored in the heap. Note that the heap and stack are two different concepts, with different storage locations in the memory. The heap is generally used for storage.

Variable-length data, such as the string type, while the stack is used to store data of fixed length, such as integer data int (each int variable

Occupies four bytes ). It can be learned from the data storage location that when one value variable is assigned to another value variable, two values are saved in the stack.

And assign a reference variable to another reference variable. The two references to the same heap location are saved in the stack.

That is, the address of the same heap is saved in the stack. During data operations, each variable has its own value type,

Therefore, operations on a variable will not affect other variables. For variables of the reference type, operations on the data of a variable are

Variables in the heap are operated. If two referenced variables reference the same object, the actual meaning is that they are saved in the stack.

The address of the heap to be stored is the same. Therefore, operations on a variable will affect another variable that references the same object.

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.