C # Value types and reference types

Source: Internet
Author: User

There is a section explaining the value types and reference types well. Excerpt from a book, as follows:

Before analyzing the value types and reference types in C #, let's say two examples to make the concept of abstraction concrete.

Suppose you are reading a book, and your friend wants to see the book in your hand at this time. For a friend to see, either lend their wounded book to the other, or copy it to him. In any case, it is the operation of the book itself, and the copy of the two books, completely independent, there is no relationship. This behavior can be analogous to the behavior of value types in C #.

For another hypothesis, let's say you're watching TV, changing stations, adjusting the volume, and so on, using the TV remote instead of directly on the TV. If your friend wants to see another TV station or change the volume, don't give him the TV, just give him the remote. This is the behavior of the reference type.

Most of C # is reference types, but in the actual development process, value types are often used. Reference types are always allocated from the managed heap, and C # requires all objects to be created using the new operator.

Difference: The biggest difference is that a variable based on a value type directly contains a value. That is, when you assign a value type variable to another value type variable, the value it contains is copied directly, and the assignment of the reference type variable is simply a reference to the copied object. To lift a chestnut, if all types are reference types, then even a number is a reference type, meaning that using a number 1 requires a huge memory allocation.

Another difference is that value types cannot derive other types, all value types are implicitly sealed, and this is done to prevent value types from being used as base types for other types.

How is the value of the variable assigned? Generally speaking, the value of a variable is assigned to a location that is related to the position where the variable is declared. The value of the local variable is always stored on the thread stack, and the value of the instance variable and the instance itself are stored in the instance store. Reference type instances and static variables are always stored on the heap.

Instances of value types are typically allocated on the thread stack instead of all, because in some cases value types may also be allocated on the managed heap.

These special cases include the elements in the array, the value Type field in the reference type, the local variable in the iterator block, and the local variable in the anonymous function (LAMDA) in the case of the closure. At this point, it is possible that if the value type instance in that case is allocated on the thread stack, there is a possibility that line stacks's method has been called to the end, but still accesses those values, the value will be erased as the call method returns. Therefore, they are allocated on the managed heap to meet the requirements that can be accessed after the method returns.

Therefore, it is not accurate to simply say "the reference type is saved on the managed heap, and the value type is saved on the thread stack." Accurately, reference types are always allocated on the managed heap, but value types are not always allocated on the thread stack.

  any type known as a "class" is a reference type . It is particularly important to note that string strings are not an actual string, but rather a reference to a string, and therefore the string is a reference type.

Common reference types in C #, such as the System.Collections.Generic.List class, the System.Text.Decoder class.

The value type is relative to the reference type, and can be divided into two types: structure and enumeration.

The structure is divided into three types:

    • Digital structure: System.Int32 structure, system.float structure, System.Decimal structure and so on.
    • Boolean structure: Common is system.boolean and so on.
    • User-defined structure.

According to the official Microsoft Language Specification document, all value types must derive from System.vlauetype. In addition, value types are represented in two ways, unboxed and boxed, respectively. And the boxing mechanism is? Converts a value type to a reference type. is because value types are not allocated in the managed heap and are not garbage collected. However, in many cases, it is necessary to obtain and manipulate references to instances of value types, when the boxing mechanism arises.

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.