First, what is a primitive type
Some data types are so common that many compilers allow code to manipulate them in simplified syntax. For example, you can use the following syntax to assign an integer:
System.Int32 a = new System.Int32 ();
But you certainly don't want to use this syntax to declare and initialize an integer, because it's too cumbersome. Fortunately, many compilers, including C #, allow the following syntax to be swapped:
int a = 0;
This syntax not only increases the readability of the code, but also generates an IL code that is exactly the same as the IL code generated when using System.Int32. The data types that this compiler supports directly are called primitive types (primitive type). Primitive types map directly to types that exist in the Framewok class library.
Basic types are also written as literal constants, and literal constants can be thought of as an instance of the type itself, so an instance method can be called as follows for an instance (123):
Console.WriteLine (123.ToString ());
In addition, if an expression is made up of literal constants, the compiler can perform the evaluation of the expression at compile time, thereby enhancing the performance of the application:
Int32 x = 100 + 20; Compiling the generated code sets X to 120
Ii. what primitive types are in C #
C # Primitive types |
FCL Type |
CLC Compatible |
Description |
SByte |
System.SByte |
Whether |
Signed 8-digit number |
Byte |
System.Byte |
Is |
Unsigned 8-digit number |
Short |
System.Int16 |
Is |
Signed 16-digit number |
UShort |
System.UInt16 |
Whether |
Unsigned 16-digit number |
Int |
System.Int32 |
Is |
Signed 32-digit number |
UInt |
System.UInt32 |
Whether |
Unsigned 32-digit number |
Long |
System.Int64 |
Is |
Signed 64-digit number |
ULong |
System.UInt64 |
Whether |
Unsigned 64-digit number |
Char |
System.Char |
Is |
16-bit Unicode characters, 2 bytes in length |
Float |
System.Single |
Is |
IEEE32 bit floating point value |
Double |
System.Double |
Is |
IEEE64 bit floating point value |
bool |
System.Boolean |
Is |
A True/false value |
Decimal |
System.Decimal |
Is |
A 128-bit high-precision floating-point number |
String |
System.String |
Is |
A character array |
Object |
System.Object |
Is |
Base types for all types |
Dynamic |
System.Object |
Is |
is exactly the same for Clr,dynamic and object. |
Iii. Conversions between types
Int32 i = 5;
Int64 L = i;
C # only allows implicit conversions when "safe" is converted. The so-called "security" refers to the situation where data loss does not occur. For example, convert from Int32 to Int64. However, if a conversion is likely to be unsafe, C # requires an explicit conversion. It is important to note that different compilers may generate different code to handle transformation operations. For example, when a single with a value of 6.8 is transformed into a Int32, some compilers might generate code, truncate it, and eventually put 6 into Int32, and some compilers might take the result up to 7.c# and always truncate the result without rounding up.
Many arithmetic operations performed on primitive types are likely to cause overflow:
Byte b = 100;
b = (Byte) (b + 200);
In most programming scenarios, this creeping overflow is something we do not want, and if this overflow is not detected, it can cause the application to behave erratically. The CLR provides special IL directives that allow the compiler to choose the behavior it deems most appropriate. The CLR has an add directive that adds two values together, but does not perform overflow checking, and the CLR has a ADD.OVF directive that adds two values together, but throws an OverflowException exception if an overflow occurs. In addition to these two IL directives for addition operations, the CLR has other operations that provide similar functionality.
C # allows programmers to decide for themselves how to handle overflows, and overflow checking is turned off by default. That is, when the compiler generates IL code, it automatically uses a version of arithmetic that does not contain overflow checking. The result is that the code can run faster. However, developers must ensure that no overflow occurs, or that their code can anticipate these overflows. One way to let the C # compiler control overflow is to use the/checked+ compiler switch to change the checked settings in Visual Studio, open the Project's Properties dialog box, select the Build tab, click Advanced, and then tick check for operational overflow/underflow. In addition to this global open or close overflow check, programmers have the flexibility to control overflow checking in specific areas of the code. C # implements this flexibility by providing the checked/unchecked operator.
Byte b = 100;
b = Checked ((Byte) (b + 200));
In addition to the checked/unchecked operator, C # supports checked/unchecked statements, which cause all expressions in the block to be checked or not overflow.
[CLR via C #] primitive type