Dark Horse Programmer -- 1C # Analysis of reference type and Value Type

Source: Internet
Author: User

ASP. Net + Android + IO development. Net training 1 Introduction

Understanding the value type and reference type is one of the important theme for understanding the basics of C # language. Here we will introduce the following content:

    • 1) memory allocation of value type and reference type
    • 2) When to use the value type and when to use the reference type.
    • 3) packing and unpacking
1.1 value type and reference type memory allocation

Value Type: The value type instance is either assigned to the stack or to the stack (in this case, the Type field exists on the managed stack ). Value types include simple types and structures. Define the value type with the keyword Struct. (Because the value types are sealed, it means that all the defined value types inherit the word ValueType and cannot be derived, but the interface can be implemented.) If the value type instance is assigned to the stack, after a method call exits, it is destroyed. If the method is located on the GC stack, it will die as the object where the method is located, which is mainly controlled by GC. Because the value type itself contains values, which are usually allocated on the stack, the access does not require address conversion and the allocated space is automatically released, so the value type is more efficient.

      Main(= = = Student(,,                                                                                         {  _firtName+ Sex _sex=                   {                     Age { ;                                                                           Student( argFirstName,  argLastName, ====                                                   

The Value Type Variable a and the instance memory referenced by the referenced type variable stu are as follows:

1.2 When is the value type used and when is the reference type used?

Reference types include behavior, polymorphism, and inheritance. We can find that, as long as struct is used, it can basically be replaced by class. So why is there a value type? The answer is efficiency. We know that the reference type is allocated to the stack, and each object on the stack has some additional members (Synchronous Index block, type object pointer (method table pointing to type object )), initialization is required. In addition, if an object is allocated from the managed stack, a garbage collection operation may be executed forcibly. It can be seen that all types of the software system are reference types, so the performance of the referenced program will be significantly reduced. Value types are usually distributed on the stack, which is usually a small data structure with numerical meanings. During access, you do not need to search by referencing type variables like the access object, it is automatically released after use, so the efficiency will be very high.

Generally, the value type should be prioritized when defining a type because it is more efficient. Value types are generally used in the following scenarios:

Type 1 contains a small amount of data and is mainly used to store data, which has obvious numerical meanings.

2 types do not need to be inherited from any other types (except interfaces), nor are any other types generated

On the contrary, when selecting a reference type, the data structure of option 1 itself is large; Option 2 may be extended (derived ). At present, my personal knowledge is limited. It seems that I can only think of these two points.

 

3 packing and unpacking

1 concept: packing and unpacking refers to the conversion between value type and reference type. Packing refers to converting the value type data into a reference Object, which can be treated as an Object. It is usually converted to the Object type or any interface reference type implemented by this value type; unpacking is the conversion of the reference type to the value type, which is usually accompanied by the operation of copying the data field of the object from the heap. Note: Only objects that have been packed in boxes can be split.

2. First read a piece of code.

{
Int x = 100;
Object o = x; // boxed
Int y = (int) o; // unpack
}

Three steps in the packing process:

  • Memory Allocation: allocate memory space in the managed heap. The memory size is the size of the value type to be packed plus additional memory space (mainly for type object pointers and Synchronous Index blocks)
  • Instance copy: copy the field value of the value type to the newly allocated memory.
  • Return reference: return the address of the object in the managed heap to the new reference type variable (that is, the object o in the example)

Binning process two steps

  • Instance check: Check whether the object is null and whether the object is a boxed value of the given value type. If the condition is not met, an exception is thrown.
  • Data Field Retrieval address: Obtain the address of the data field from the instance (in this example, the address where o stores 100)
  • Copy value: copy the value to the stack-based value type instance based on the obtained data field address (y in this example)

Summary: packing and unpacking will cause a lot of performance losses. We should try to use the overload method or generic type provided by FCL when writing programs. For example, StringBuilder is often used to splice a large number of strings, which is much higher than "+.

ASP. Net + Android + IOS development. Net Training

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.