Value types and reference types

Source: Internet
Author: User

Let's take a brief look at them:

Strictly speaking, these are the following: value types include: struct (numeric type, bool type, user-defined struct), enumeration, nullable type.

Reference types include: array, user-defined class, interface, delegate, object, string

Why Design value types:

    1. Value types do not need to be allocated from the managed heap
    2. instance objects produced by reference types will have additional members on the managed heap, which must be initialized
    3. Value types do not use GC garbage Collection control, and they are released on their own when the scope ends.

In general, value types are more efficient, so setting some simple, common, memory-intensive objects to value types in. NET greatly improves the efficiency of the entire CLR .

Packing and unpacking:

Why boxing occurs:

because value types differ from how memory is managed by reference types in the CLR , boxing occurs when the type of the values needs to be converted to a reference type in the program. That is, the instance object of the value type needs to be converted into another type of object that does not inherit from System.ValueType in a program-scoped scenario, and boxing occurs.

What is the danger of boxing:

because the design value type is intended to improve efficiency. However, when boxing occurs, the value type is converted into a reference type, and this time, the efficiency has not been improved. Instead, because of the boxing, the unboxing may be repeated several times, instead of making efficiency more affected.

The difference between a value type and a reference type:

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 the memory unit only holds the objects in the heap.
Address.

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

The data is stored in the heap. Note that heap and stack are two different concepts, and storage locations in memory are not the same, and heaps are typically used to store

Variable-length data, such as string types, while stacks are used to store fixed-length data, such as int types (int variable

occupies four bytes). The location of the data store informs you that when assigning a value variable to another value variable, two

Identical value, and assigning a reference variable to another reference variable saves two references to the same heap location on the stack

, that is, the address of the same heap is saved in the stack. In the case of data manipulation, for value types, because each variable has its own value,

Therefore, the operation of one variable does not affect other variables, and for variables of reference type, manipulating the data of a variable is

Variables in the heap, if the variables of the two reference types refer to the same object, the actual meaning is that they are guaranteed in the stack

The address of the stored heap is the same, so the operation on one variable affects another variable that references the same object.

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.