The C# Predefined Types
The C# language predefines a set of types that map to types in the common type system. If you are familiar with another programming language, the names of these types might be different, but you can easily see the correlation. All the predefined types are value types except for object and string. The predefined types are shown in Table 3.1.
Table 3.1. Predefined C# Types
Keyword |
Aliased Type |
Description |
Range |
bool |
Boolean |
Logical Boolean |
true or false |
byte |
Byte |
Unsigned 8-bit integer |
0 to 255 |
char |
Char |
A single 16-bit Unicode character |
U+0000 to U+FFFF |
decimal |
Decimal |
A 128-bit data type with 28–29 significant digits |
(–7.9 × 1028 to 7.9 × 1028) / (100 to 28) |
double |
Double |
Double-precision 64-bit floating point up to 15–16 digits |
±5.0 × 10–324 to ±1.7 × 10308 |
float |
Single |
Single-precision 32-bit floating point up to 7 digits |
±1.5 × 10–45 to ±3.4 × 1038 |
int |
Int32 |
Signed 32-bit integer |
–231 to 231 – 1 |
long |
Int64 |
Signed 64-bit integer |
–263 to 263 – 1 |
sbyte |
SByte |
Signed 8-bit integer |
–128 to 127 |
short |
Int16 |
Signed 16-bit integer |
–32,768 to 32,767 |
uint |
UInt32 |
Unsigned 32-bit integer |
0 to 4,294,967,295 |
ulong |
UInt64 |
Unsigned 64-bit integer |
0 to 18,446,744,073,709,551,615 |
ushort |
UInt16 |
Unsigned 16-bit integer |
0 to 65,535 |
object |
Object |
Base type of all other value and reference types, except interfaces |
N/A |
string |
String |
A sequence of Unicode characters |
N/A |
By including a type to directly represent Boolean values (values that are either true or false), there is no ambiguity that the value is intended to be a Boolean value as opposed to an integer value. This helps eliminate several common programming errors, making it easier to write self-documenting code.
The decimal type provides at least 28 significant digits and is designed to have no representation error over a wide range of values frequently used in financial calculations. The range of values the double type can represent with no representation error is a set used primarily in physical calculations.
The object type is the underlying base type for all the other reference and value types. The string type represents a sequence of Unicode code units and cannot be changed once given a value. As a result, values of type string are immutable.
C# also has some special types, the most common being the void type. The void type indicates the absence of a type. The dynamic type is similar to object, with the primary difference being all operations on that type will be resolved at runtime rather than compile time.
Although void and dynamic are types, var represents an implicitly typed variable and tells the compiler to determine the real type based on the assigned data.