C # provides some predefined types, most of which are familiar to C and C ++ programmers. The predefined reference types are object and string. The object type is the final base class of all types. The string class represents a Unicode string. The value of the string object is unchangeable.
Predefined value types include signed and unsigned integer, floating-point, and bool, char, and decimal. Signed integer types include sbyte, short, int, long; unsigned integer types include byte, unshort, uint, and ulong; floating point types include float and double.
Bool is used to indicate a Boolean value: it can only be true or false. The bool type makes it easier to write self-description code and eliminates Common Errors in this C ++ Code: "=" is used in the place where "=" is used ". In c #, the Code is as follows:
Int I = ...;
F (I );
If (I = 0) // Bug: the test shocould be (I = 0)
G ();
Compilation errors may occur because the expression I = 0 is of the int type, but the if statement requires a bool expression.
The char type is used to represent the Unicode encoding sequence. The char type variable represents a 16-bit Unicode encoding sequence.
The decimal type is used when rounding errors caused by floating-point operations become unacceptable, usually including financial computing such as tax calculation and currency exchange. Decimal provides at least 28 valid digits.
The following table lists these predefined types and their constant values.
Type |
Description |
Example |
Object |
All other types of final base classes |
Object o = null; |
String String type; |
A string is a Unicode encoding sequence. |
String s = "hello "; |
Sbyte |
8-bit signed integer type |
Sbyte val = 12; |
Short |
16-bit signed integer |
Short val = 12; |
Int |
32-bit signed integer |
Int val = 12; |
Long |
64-bit signed integer |
Long val1 = 12; Long val2 = 34L; |
Byte |
8-bit unsigned integer type |
Byte val1 = 12; |
Ushort |
16-bit unsigned integer |
Ushort val1 = 12; |
Uint |
32-bit unsigned integer |
Uint val1 = 12; Uint val2 = 34U; |
Ulong |
64-bit unsigned integer |
Ulong val1 = 12; Ulong val2 = 34U; Ulong val3 = 56L; Ulong val4 = 78UL; |
Float |
Single precision floating point type |
Float val = 1.23F; |
Double |
Dual-precision floating point |
Double val1 = 1.23; Double val2 = 4.56D; |
Bool |
Bill type. The value can only be true or false. |
Bool val1 = true; Bool val2 = false; |
Char |
Character type, indicating the Unicode encoding sequence |
Char val = 'H '; |
Decimal |
Precise decimal type, with at least 28 valid digits |
Decimal val = 1.23 M; |
Each predefined type corresponds to a system-provided type. For example, the keyword indicates the structure type of System. Int32. As a encoding style, the complete system type name is generally replaced by a keyword.
Predefined types are specially processed in some aspects, but they are similar to other structural types. Operator Overloading allows developers to define new structures that are similar to predefined types. For example, a Digit structure type can support all operators supported by predefined types, and can define conversion from predefined types to Digit types.
By default, operators are overloaded for predefined types. For example, the comparison operators = and! = Different predefined types have different meanings:
If two int-type expressions contain the same integer value, they are equal.
The expressions of two object types reference the same object, or both are null, so they are equal.
The strings referenced by two string expressions are of the same length and the characters corresponding to each position are the same, or both are null, so they are equal.
See the example below:
Using System;
Class Test
{
Static void Main (){
String s = "Test ";
String t = string. Copy (s );
Console. WriteLine (s = t );
Console. WriteLine (object) s = (object) t );
}
}
The output is:
True
False
The reason is that the object of the first comparison operation is two string-type expressions, and the object of the second comparison operation is two object-type expressions.
(Note: The Standard Library generates a string representation for the boolean type, as shown in the above System. console. writeLine, the output is "True" and "False", while the two constants of the Standard C # Boolean Value write true and false ).