Predefined Types
This section explains each of C#’s predefined types:
Value types
Integer, signed (
sbyte
,short
,int
,long
)Integer, unsigned (
byte
,ushort
,uint
,ulong
)Floating-point (
float
,decimal
,char
,bool
)
Reference types
Object
String
All of these 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 -2 n-1 to 2 n-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 chosen goes in this order: int
, uint
, long
, and ulong
.
The following suffixes may be used to explicitly specify the chosen type:
- U
uint
orulong
- L
long
orulong
- UL
ulong
Integral conversions
An implicit conversion between integral types is permitted when the type to convert to contains every possible ...
Get C# in a Nutshell 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.