".NET Framework Design" Chapter 5 Bench Type, Reference Type and Value Type

xiaoxiao2021-03-06  72

First, the primitive type (Primitive Type): The data type directly supported by the compiler.

Reference Type: Always allocate from the hosted stack. The recycling of memory passes through the garbage collector.

Value Type: Typically allocated in the stack of the thread (of course, can also be embedded in an object of a reference type). Value type instance is not controlled by the garbage collector.

Second, try to use the FCL type name: In the development, try to use the FCL type name, which is System.String, System.InT32, etc. Not to use String, int and other primitive type names. Because the FCL type name can be seen in some limits, if INT32 is a symbol number of 32 bits, it is more intuitive; in addition, some primitive types and corresponding FCL type names are not the same, easy to cause confusion, such as the primitive Type FLOAT's FCL type name is System.single; Final, the same primitive type may correspond to different FCL types in different programming languages, which will bring a range of potential issues.

Third, type conversion: You can use the system.convert type static method to transform between different types of objects. The static method in the Convert type ChangeType can convert a type of type to another, provided that the converted type implements the iconvertible interface, especially the TOTYPE method. (Can this technology can be used to achieve something new ??)

Fourth, the overflow exception control: Many of the type of primitive type arithmetic operation will result in overflow, default, this overflow does not throw an exception, you can use the / checked command line switch to tell the compiler to use overflow IL instructions to generate code. (How to open this switch in the development environment) Another method is to use the check and unchecked operator to switch a spill check for a code. I personally feel that there is no need to use the Checked and Unchecked operators to do this fine control, although the overflow detection development directly open the compiler will reduce the performance of some applications, but this impact can not be considered.

V. Quote Types and value types: any "class" is a reference type, any "structure" and "enumeration" are all value types. In addition, all value types must be inherited from the System.ValeType type. The CLR does not allow a value type as a base type.

Six, value type packages and unpack: When needed to improve the performance performance of a program, you can analyze this program, minimize the loading and unpacking operation. The reason why many methods provide overloaded versions, one of the purposes is to reduce packing operations of some common value types.

转载请注明原文地址:https://www.9cbs.com/read-120729.html

New Post(0)