Numeric Types
C# has the following predefined numeric types:
C# type | System type | Suffix | Size | Range |
---|---|---|---|---|
Integral—signed | ||||
|
| 8 bits | –27 to 27–1 | |
|
| 16 bits | –215 to 215–1 | |
|
| 32 bits | –231 to 231–1 | |
|
|
| 64 bits | –263 to 263–1 |
Integral—unsigned | ||||
|
| 8 bits | 0 to 28–1 | |
|
| 16 bits | 0 to 216–1 | |
|
|
| 32 bits | 0 to 232–1 |
|
|
| 64 bits | 0 to 264–1 |
Real | ||||
|
|
| 32 bits | ± (~10–45 to 1038) |
|
|
| 64 bits | ± (~10–324 to 10308) |
|
|
| 128 bits | ± (~10–28 to 1028) |
Of the integral types, int
and long
are first-class citizens and are favored by both C# and the runtime. The
other integral types are typically used for interoperability or when space
efficiency is paramount.
Of the real number types, float
and double
are called floating-point
types and are typically used for scientific calculations. The
decimal
type is typically used for
financial calculations, where base-10-accurate arithmetic and high
precision are required. (Technically, decimal
is a floating-point type too, although
it’s not generally referred to as such.)
Numeric Literals
Integral literals can use decimal or hexadecimal notation; hexadecimal is denoted with the 0x
prefix (e.g., 0x7f
is equivalent to 127
). Real literals may
use decimal or exponential notation such as 1E06
.
Numeric literal type inference
By default, the compiler infers a numeric
literal to be either double
or an
integral type:
If the literal contains a decimal point or the exponential symbol (
E
), it is adouble
.Otherwise, the literal’s type is ...
Get C# 5.0 Pocket Reference now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.