[CLR via C #] 5.2 primitive type

Source: Internet
Author: User

CLR supports two types: reference type and value type.

Although most FCL data types are reference data types, most developers use value types. The reference type is always allocated on the hosting stack. The new operator of C # returns the memory address of the object, that is, the memory address pointing to the object data. When using the reference type, you must note some performance issues. first consider the fact: 1) the memory must be allocated from the managed stack. 2) There are some additional members for each assigned object (as mentioned above" Type object pointer"And" Synchronize block Indexes"), These Members must be initialized. 3) Other bytes (set for fields) in the object are always set to zero. 4) when an object is allocated from the managed stack, a garbage collection operation may be executed forcibly. If all types are reference types, the application performance will be significantly reduced. To improve the performance of simple and common types, CLR provides a lightweight type named "value type. Value-type instances are generally allocated on the thread stack (although they can also be embedded into a reference type object as fields ). A variable in an instance that represents a value type does not contain a pointer to an execution instance. On the contrary, variables contain fields of the instance. Because the variable already contains the instance field, you do not need to provide a pointer to operate the field in the instance. Value-type instances are not controlled by the garbage collector. Therefore, the use of value types relieves the pressure on the managed heap and reduces the number of garbage collections that an application needs to perform during its lifetime. The. NET Framework SDK documentation clearly states that when you view a type, any type that becomes a "class" is a reference type. Such as the System. Exception class and System. Random class. The document converts all value types into structures or enumeration. Such as the System. Int32 structure and System. Boolean structure. All value types must be derived from System. ValueType. All enumeration types are derived from the System. Enum abstract class, while System. Enum is derived from System. ValueType. CLR and all programming languages are given special enumeration treatment, which will be mentioned later. All value types are implicitly sealed to prevent a value type from being used as the base type of any other reference type or value type. In managed code, developers who define a type decide where to allocate an instance, and those who use this type have no control over this. The following shows the differences between the reference type and value type:
  Main(=  SomeRef();         SomeVal v1 =  SomeVal();         r1.x = =     Console.WriteLine(v1.x);                === =     Console.WriteLine(r2.x);                    Console.WriteLine(v1.x);                    Console.WriteLine(v2.x);                } 

1) The type has the behavior of the primitive type. 2) The type does not need to inherit from any other type. 3) the type does not derive from other types. The size of a type instance should be taken into consideration, because by default, real parameters are transmitted as values, which will result in copying fields in the Value Type instance, thus affecting performance. Similarly, when a method is defined to return a value type, the fields in the instance are assigned to the memory allocated by the caller, thus affecting performance. Therefore, the value type should also meet the following requirements: 1) instances of the type are smaller (about 16 bytes or smaller). 2) instances of the type are larger (more than 16 bytes ), but it is not passed as the real parameter of the method, nor is it returned from the method. The main advantage of value types is that they are not allocated as objects on managed stacks. Difference between value type and reference type: 1) value type objects have two forms: unboxed and boxed ). The reference type is always in the boxed form. 2) The value type is derived from System. ValueType. This type provides the same method as defined by System. Object. However, System. ValueType overrides the Equals and GetHashCode methods. Because this default implementation has performance problems, you should override the Equals and GetHashCode methods when defining your own value types and provide their display implementation. 3) all methods of the value type cannot be abstract, and all methods are implicitly sealed methods. 4) variables of the reference type contain the address of an object on the stack. By default, when a variable of the reference type is created, it is initialized to null, indicating that the variable of the reference type does not currently point to a valid object. Conversely, the value type Initialization is that all members are initialized to 0. Because the value type variable is not a pointer, when accessing a value type, NullReferenceException is not thrown. CLR does provide a special feature to add a "null" identifier to the value type. For example, "int? "5) assign a value type variable to another value type variable, and perform a copy by field. When a reference type is assigned to another reference type, only the memory address is copied. 6) because the boxed value type is no longer allocated on the heap, once the method of defining an instance of this type is no longer active, the memory allocated to them will be recycled.

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.