Compare the difference between a value type and a reference type in C #

Source: Internet
Author: User
The CLR supports two types: value types and reference types, and it appears that most of the types of FCL are reference types, but most of them are value types. The reference type is always allocated from the managed heap, and an object is returned with the new operator, and the memory address of the object is stored in a variable. There are four psychological factors to be aware of when using reference types:

1. Memory must be allocated from the managed heap

2. Each object allocated on the heap has some extra members that must be initialized before they are used.

3. The other bytes in the object are always set to zero.

4. When an object is allocated from the managed heap, a garbage collection may be enforced.

This shows that the abuse of reference type is bound to cause overload. The value type, which is a lightweight type, is also embedded in the reference type object on the general allocation thread stack, the variable that hosts it does not contain pointers to the instance, and of course is not controlled by the garbage collector, so the use of value types mitigates the pressure on the managed heap, reducing the number of garbage collections during the lifetime of the application.

C # is referred to as a ' class ', and a value type is a struct or an enumeration. All structures are derived directly from the abstract type System.ValueType. And it derives directly from object, and all enumerations derive from the System.Enum abstract type, and enum derives from the ValueType, interlocking.

When you design your own type, when will it be defined as a value type?

1. Types have primitive type behavior (data types that are directly supported by the compiler (like Int32,int64, etc.))

2. Type does not need to inherit from any other type.

3. The type does not derive any other type.

4. Because the value type is used as an argument or return value, it is necessary to copy the corresponding field, if the instance is too large, it will cause some performance loss.

So it is required that the type instance is smaller (within 16 bytes), the instance is large, but not passed as a method argument and returned from the method.

The differences between value types and reference types are outlined in a few points:

1. The reference type is in the boxed form, the value type is in an unboxed form, and the two can be converted to each other in a certain way (details will be discussed in the next blog post)

2. Value types should not introduce any new virtual methods, all methods cannot be abstract and are implicitly sealed to prevent rewriting.

3. Assigning a value type variable to another value type variable performs a verbatim copy of the segment. The variable of the reference type is paid to another reference type, and only the memory address is copied. Two or more reference-type variables can refer to the same object on the heap, and operations on one variable may affect the object referenced by another variable. Instead, value-type variables are independent and do not affect each other.

4. The unboxed value type is not allocated on the heap, so once an instance of that type is not active, the storage allocated for them is freed and does not wait for garbage collection
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.