Predefined Types
All
of C#’s predefined types
alias types found in the System namespace. For
example, there is only a syntactic difference between these two
statements:
int i = 5; System.Int32 i = 5;
Integral Types
This table lists the integral types and their features:
|
C# type |
System type |
Size |
Signed |
|---|---|---|---|
|
|
|
1 byte |
Yes |
|
|
|
2 bytes |
Yes |
|
|
|
4 bytes |
Yes |
|
|
|
8 bytes |
Yes |
|
|
|
1 byte |
No |
|
|
|
2 bytes |
No |
|
|
|
4 bytes |
No |
|
|
|
8 bytes |
No |
For unsigned integers that are n bits wide, possible values range from 0 to 2. For signed integers that are n bits wide, their possible values range from -2n-1 to 2n-1-1. Integer literals can use either decimal or hexadecimal notation:
int x = 5; ulong y = 0x1234AF; // prefix with 0x for hexadecimal
When an integral literal is valid for several possible integral
types, the default type that is chosen goes in this order:
int, uint,
long, and ulong. The following
suffixes may be used to specify the chosen type explicitly:
- U
uintorulong- L
longorulong- UL
ulong
Integral conversions
An implicit
conversion between integral types is
permitted when the type to which you’d like to
convert contains every possible value of the type to convert.
Otherwise, an explicit conversion is required. For instance, you can
implicitly convert an int to a
long, but must explicitly convert an
int ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access