[CLR via C #] reference types and value types

Source: Internet
Author: User

One, the difference between a reference type and a value type

The CLR supports two types: reference types and value types. The reference type is always allocated from the managed heap, and the new operator of C # returns the memory address of the object. When using reference types, you must be aware of some performance issues.

1) The memory must be allocated from the managed heap.

2) Each object allocated on the heap has some additional members (type Object pointers and synchronous index blocks) that must be initialized.

3) The other bytes in the object (set for the field) are always set to zero.

4) When an object is allocated from managed urbanisation, a garbage collection operation may be enforced.

To improve the performance of simple, common types, the CLR provides a lightweight type named value type. An instance of a value type is typically allocated on the thread stack (although it can also be embedded as a field in an object of a reference type). In an instance that represents a value type, it does not contain a pointer to an instance. Instead, the variables contain the fields of the instance itself. Because the variable already contains the field of the instance, it is no longer necessary to extract a pointer in order to manipulate the field in the instance. Instances of value types are not controlled by the garbage collector. Therefore, the use of value types mitigates the pressure in the managed heap and reduces the number of garbage collections that an application needs to make during its lifetime.

Ii. conditions for declaring a type as a value type

When designing your own type, consider carefully whether you should define a type as a value type, rather than as a reference type. At some point, value types can provide better performance. Specifically, you should not declare a type as a value type unless all of the following conditions are true.

1) The type has primitive type behavior. In other words, this is a very simple type, where no member modifies any instance field of the type. It is recommended that all fields of a value type be declared as ReadOnly.

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

3) type is not derived from any other type.

The size of the type instance should also be considered. Because, by default, the arguments are passed by value, this causes the fields in the value type instance to be copied, which can compromise performance. Similarly, a method that is defined to return a value type when returned, the fields in the instance are copied into the memory allocated by the caller, thereby compromising performance.

Advantages and disadvantages of value types

The main advantage of value types is that they are not allocated as objects on the managed heap. As a value type, there are some limitations of its own, as compared to reference types.

1) There are two representations of value type objects: unboxed and boxed form.

2) Because you cannot define a new value type or a new reference type as a base type for a value type, you should not introduce any new virtual methods in the value type. Therefore, methods cannot be abstract, and all methods are implicitly sealed.

3) Assigning a variable of a value type to another value type variable will perform a verbatim copy of the segment. When you assign a variable of a reference type to a variable of another reference type, only the memory address is copied.

[CLR via C #] reference types and value 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.