Differences between C # value types and reference types

Source: Internet
Author: User

Differences between C # value types and reference types

1. Data of value type is stored in the stack of memory, the data of the reference type is stored in the heap of memory, and only the address of the object in the heap is stored in the memory unit.

2. Value type access speed is fast, reference type access speed is slow.

3. Value types represent actual data, and reference types represent pointers or references to data stored in the memory heap

4. Value types inherit from System.ValueType, reference types inherit from System.Object

5. The memory allocation of the stack is automatically released, while the heap is in. NET, there will be a GC to release

6. A variable of value type directly holds the actual data, whereas a variable of the reference type holds the address of the data, that is, the object's reference.

7. A value type variable stores the value of a variable directly on the stack, and a variable of the reference type holds the address of the actual data on the stack, while the actual data is stored in the heap.

Note that heap and stack are two different concepts, where storage is not the same in memory, heaps are typically used to store variable-length data, such as string types, while stacks are used to store fixed-length data, such as Integer type data int (each int variable occupies four bytes). Where the data is stored, it is known that when assigning a value variable to another value variable, two identical values are saved on the stack, and a reference variable is assigned to another reference variable, and two references to the same heap location are saved on the stack, that is, the address of the same heap is saved on the stack. In the case of data manipulation, for value types, because each variable has its own value, the operation of one variable does not affect other variables, and for a variable of reference type, manipulating the data of a variable is the operation of the data in the heap, if a variable of two reference types refers to the same object, The actual meaning is that they hold the same address of the heap on the stack, so manipulating one variable affects another variable that references the same object.

Increase understanding

Differences between C # 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.