Chapter V. NET Framework programming, primitive type, reference type and Value Type

Source: Internet
Author: User

I,Primitive type(Primitive type):Data Types directly supported by the compiler.

Reference Type(Reference type):It is always allocated from the managed stack. The memory is recycled by the garbage collector.

Value Type(Value type):It is usually allocated in the thread stack (of course, it can also be embedded into a reference type object ). Value Type instances are not controlled by the garbage collector.

 

II,Try to useFCLType name:During development, try to use the FCL type name, that is, system. String, system. int32, and so on. Instead of using primitive types such as string and Int. Because the FCL type name can see some limitations of the type, for example, int32 indicates the 32-bit signed number. In addition, some primitive types and the corresponding FCL type names are not the same and may cause confusion. For example, the FCL type name corresponding to the primitive type float is system. single; finally, the same primitive type may correspond to different FCL types in different programming languages, which brings about a series of potential problems.

III,Type conversion:You can use static methods of the system. Convert type to transform objects of different types. The static method changetype in the convert type can convert one type to another, provided that the iconvertible interface is implemented by the conversion type, especially the totype method. (Can I use this technology to implement some new functions ??)

IV,Overflow exception Control:Many arithmetic operations of the primitive type will cause result overflow. By default, such overflow will not throw an exception, you can use the/checked + command line switch to tell the compiler to use the Il command with overflow to generate code. (How do I enable this function in the development environment ??) Another method is to use the checked and unchecked operators in the code to enable the overflow check of a piece of code. I personally think there is no need to use the checked and unchecked operators for this fine-grained control. Although directly opening the compiler's overflow detection development will reduce the performance of some applications, this impact does not need to be considered.

V,Differences between the reference type and value type:All "classes" are reference types, and any "structure" and "enumeration" are value types. In addition, all value types must inherit from the system. valuetype type. CLR does not allow a value type to be used as the base type.

VI,Packing and unpacking of value types:To improve the execution performance of a program as much as possible, you can analyze the program to minimize the number of packing and unpacking operations. One of the purposes of many methods is to reduce the number of common value-Type Packing operations.

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.