C# contains two general categories of built-in data types: value types and reference types. C#’s reference types are defined by classes, and a discussion of classes is deferred until later. However, at the core of C# are its 13 value types, which are shown in Table 3-1. These are built-in types that are defined by keywords in the C# language, and they are available for use by any C# program.
Type | Meaning |
---|---|
bool | Represents true/false values |
byte | 8-bit unsigned integer |
char | Character |
decimal | Numeric type for financial calculations |
double | Double-precision floating point |
float | Single-precision floating point |
int | Integer |
long | Long integer |
sbyte | 8-bit signed integer |
short | Short integer |
uint | An unsigned integer |
ulong | An unsigned long integer |
ushort | An unsigned short integer |
The term value type indicates that variables of these types contain their values directly. (This differs from reference types, in which a variable contains a reference to the actual value.) Thus, the value types act much like the data types found in other programming languages, such as C++. The value types are also known as simple types. The term primitive type is also occasionally used.
C# strictly specifies a range and behavior for each value type. Because of portability requirements, C# is uncompromising on this account. For example, an int is the same in all execution environments. There is no need to rewrite code to fit a specific platform. While strictly specifying the size of the value types may cause a small loss of performance in some environments, it is necessary in order to achieve portability.
Note | C# 2.0 added a new feature called a nullable type, which enables a variable to hold an undefined value. A nullable type can be created for any value type, including the built-in types. Nullable types are discussed in Chapter 19. |