[Deep Dive into the. NET platform] talking about the. NET Framework primitive type,. netframework

Source: Internet
Author: User

[Deep Dive into the. NET platform] talking about the. NET Framework primitive type,. netframework

  • What is a primitive type?

Beginners may have seldom heard of this term, but the most commonly used is the primitive type. Let's take a look at the following two lines of code:

System.Int32 a = 5;int a = 5; 

Both the above two lines of code declare an int type variable, but we usually use the second method when writing code. The second method is concise and easy to read, and the generated IL code is exactly the same as the first one. The data types directly supported by such compilers are calledPrimitive type. Similarly, there are double, bool, long, string, and so on.

  • Relationship between primitive types and. NET Framework Class Libraries

In my first interview question, I remember the following question: what is the difference between string and String in C?

I was forced at the time. Is there a difference? It seems that there is no difference in use. In fact, there is a difference between the two. It is true that there is no difference. Because the sting primitive type is directly mapped to the String type in FCL. So they are essentially the same thing.

In C #, the primitive types are directly mapped to the existing types in the. NET Framework class library. I read a book about this ing:

using string = System.String;

From this perspective, we can think that the C # compiler assumes that all source files are added with the using command.

The following lists the types of the C # primitive types in FCL:

C # primitive type FCL type Compliant with CLS Description
Sbyte System. SByte No Signed 8-digit integer
Byte System. Byte Yes Unsigned 8-digit integer
Short System. Int16 Yes 16 is an integer.
Ushort System. UInt16 No Unsigned 16-digit integer
Int System. Int32 Yes Signed 32-bit integer
Uint System. UInt32 No Unsigned 32-bit integer
Long System. Int64 Yes Signed 64-bit integer
Ulong System. UInt64 No Unsigned 64-bit integer
Char System. Char Yes 16-bit Unicode characters
Float System. Single Yes 32-bit floating point number
Double System. Double Yes 64-bit floating point number
Bool System. Boolean Yes True/false
String System. String Yes Character array
Object System. Object Yes Base types of all types

 

 

 

 

 

 

 

 

 

 

 

 

 

 

In FCL, as long as the common language specification (CLS) type is met, other languages provide similar primitive types, but do not comply with CLS type, other languages do not necessarily support it.

  • Use of primitive types

C # The Compiler supports modes related to type conversion, literal value, and operators.

First, the compiler can perform implicit or explicit conversions between primitive types. For example:

Int a = 5; // implicitly converted from Int32 to Int32long B = a; // implicitly converted from Int32 to Int64byte d = (byte); // convert from Int32 to Byte short e = (short) a; // convert from Int32 to Int16

System. Int64 and System. Int32 are of different types and do not have a derivative relationship. So why is implicit conversion between them? The reason is that the C # compiler is very familiar with primitive types and will apply its own special rules when compiling code. That is to say, the compiler can recognize common programming patterns.

However, C # allows implicit conversion only when the conversion is secure. The so-called security means that data will not be lost. For example, Int32 is converted to Int64. However, if it is not safe, C # requires a display conversion. Unsafe numeric values mean that precision or order of magnitude may be lost after conversion. For example, Int32 to Byte. Int32 indicates the number of 32-bit binary symbols. The maximum value is 2 ^ 16-1, and Byte indicates the number of 8-bit binary symbols without symbols. The maximum value is: 2 ^ 8-1, so large Int32 may lose precision.

In addition to transformation, the primitive type can also be written into the literal value. The literal value can be an instance of the primitive type. So you can call the instance method, for example:

String a = 123. ToString (); // Int32 instance 123 call the ToString () method

In addition, if an expression is composed of a literal value, the compiler can evaluate the expression at compilation. For example:

Int a = 100 + 20 + 3; // The generated code sets the value of a to 123 string B = "a" + "bc "; // The generated code sets the value of B to "abc"

Finally, the compiler knows how and in what order to parse the operation between the primitive type and operator in the code, for example:

Int a = 5; // value assignment operation int B = a + 10; // Add and value assignment operation bool c = (a <0); // values smaller than and value assignment operations

 

 

 

References: CLR via C # (version 4)

 

PS: new users are learning the. NET Framework. I hope you can correct any errors!

 

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.