[CLR via C #] 5.1 primitive type

Source: Internet
Author: User

Some data classes are very commonly used in development, so many compilers allow the code to operate on them with simplified syntax. For example, you can use the following syntax to assign an integer:

System.Int32 a = new System.Int32();

  

int a = 0;

 

Primitive type(Primitive type ). The primitive type is directly mapped to the type existing in the Framework class library (FCL. For example, the following four lines of code are correct, and the generated IL code is the same.
int a = 0;System.Int32 a =0;int a = new int();System.Int32 a = new System.Inte32();

The following table lists the primitive types of the FCL type in C:

C # primitive type FCL type CLS compatibility Description
Sbyte System. SBte NO Signed 8-bit value
Byte System. Byte YES Unsigned 8-bit value
Short System. Int16 YES Signed 16-bit value
Ushort System. UInt16 NO Unsigned 16-bit value
Int System. Int32 YES Signed 32-bit value
Uint System. UInt32 NO Unsigned 32-bit value
Long System. Int64 YES Signed 64-bit value
Ulong System. UInt64 NO Unsigned 64-bit value
Char System. Char YES 16 is a Unicode Character
Float System. Single YES IEEE32-bit floating point value
Double System. Double YES IEEE64-bit floating point value
Bool System. Boolean YES A value of true/false
Decimal System. Decimal YES A 128-bit high-precision floating point value, which is often used in financial calculation that cannot be included in the error
String System. Strign YES One character array
Object System. Object YES Base types of all types
Dynamic System. Object YES For CLR, dynamic and object are exactly the same. However, the C # compiler allows a simple syntax to involve dynamic variables in dynamic scheduling.

 

As you can imagine, the C # compiler automatically assumes that the following using commands are added to all source code files:

using sbyte = System.Sbyte;using byte = System.Byte;using int = System.Int32;using uint = System.UInt32;......

C # in terms of language specifications: "in terms of style, it is best to use keywords instead of complete system type names ". However, the author of this book does not agree with this statement. The following are some of his reasons:

1) Many developers are confused about whether to use string or String. Since the string (keyword) of C # is directly mapped to System. String (an FCL type), there is no difference between the two. Some developers believe that int Is a 32-bit integer in a 32-bit system, and it becomes a 64-bit integer in a 64-bit system. This is not the case. In C #, the int value is always mapped to System. Inte32. In all systems, the int value is a 32-bit integer. If Int32 is used, this misunderstanding will not occur.

2) in C #, long is mapped to System. Int64. In other programming languages, long may be mapped to Int16 or Int32. In this way, it is easy to misunderstand other programming languages.

BinaryReader br = new BinaryReader (...); float val = br. readSingle (); // correct when it looks unnatural Single val = br. readSingle (); // correct, looks clear
4) at ordinary times, many programmers who only use C # gradually forget that they can use other languages to write CLR-oriented code. As a result, "C # doctrine" has penetrated the class library code. Executing many arithmetic operations on the primitive type may cause overflow. Different Languages have different overflow processing methods. C and C ++ do not regard overflow as an error and allow rollback. Applications "if nothing happens" run. On the contrary, Microdsoft Visual Basic always regards overflow as an error and throws an exception. CLR provides some special IL commands that allow the compiler to choose the behavior that it deems most appropriate. CLR has an add command that adds two values but does not check for overflow. The add. ovf command is used to add two values and throw an exception when overflow occurs. There are also sub/sub. ovf and so on. C # allow developers to determine how to handle overflow. Overflow check is disabled by default. One way developers can use the C # compiler to control overflow is to use the/checked + compiler switch. C # provides the checked and unchecked operators to check whether local overflow occurs.
Unchecked: UInt32 invalid = unchecked (UInt32) (-1); // OKchecked: Byte B = 100; B = checked (Byte) (n + 200 )); // throw an overflow exception

  

checked{    Byte b = 100;    b = checked((Byte)(n+200));}

In the "advanced generation settings" dialog box of Visaul Studio, you can specify whether the compiler checks overflow.

The System. Decimal type is a very special type. Although C # regards Decimal as a primitive type, CLR does not. That is to say, CLR does not have the corresponding IL command to determine how to process the Decimal value. The Decimal value processing speed is slower than the value processing speed of other CLR primitive types. For Decimal, the checked and uncheked operators, statements, and compilers are invalid. When Decimal overflows, an exception is thrown.

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.