Deep understanding of differences between value types and reference types

Source: Internet
Author: User

Differences:
1. The value type is usually allocated to the stack, and its variables directly include instances of variables, which is highly efficient.
2. The reference type is assigned to the managed stack. A variable of the reference type usually contains a pointer to the instance through which the variable references the instance.
3. The value type inherits from ValueType (Note: System. ValueType inherits from System. Object), while the reference type inherits from System. Object.
4. Value Type variables include instance data. Each variable stores its own copy of data. Therefore, by default, parameter passing of value type does not affect the parameter itself; the reference type variable saves the reference address of its data, so the parameter itself will be affected when passing parameters in reference mode, because the two variables will reference the same address in memory.
5. There are two types of values: binning and unboxing. The reference type can only be boxed. I will discuss this topic in detail in the next section.
6. Typical value types include struct, enum, and a large number of built-in value types. classes can be referred to as reference types.
7. The memory of the value type is not controlled by GC (garbage Collection, Gabage Collection). When the scope ends, the value type is automatically released, reducing the pressure on the managed heap, therefore, it has performance advantages. For example, struct is generally more efficient than class, and GC is used to recycle reference-type memory. Microsoft even recommends that you do not release the memory on your own.
8. The value type is sealed. Therefore, the value type cannot be a base class of any other type, but can be a single inheritance or multiple inheritance interfaces. The reference type generally has inheritance.
9. The value type does not have polymorphism, but the reference type has polymorphism.
10. The value type variable cannot be a null value. The value type is initialized to a value of 0. By default, the referenced type variable is created as a null value, indicates that there is no reference address pointing to any managed heap. An NullReferenceException is thrown for any operation of the reference type whose value is null.
11. The value type has two statuses: boxed and unboxed. The runtime provides the boxed form of all value types, while the reference type usually has only one format: boxed.

Related Article

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.