C# Reference ยท Types and Literals
C# unsigned types: uint, ulong, ushort and sbyte
Reference for C# uint, ulong, ushort and sbyte: ranges, literal suffixes, wrap-around, mixing signed and unsigned values, and when to use them.
C# has four integer types that sit beside the everyday int and long. uint, ulong and ushort hold only zero and positive values, which doubles the positive range compared with their signed twins. sbyte is the odd one out: it is the signed 8-bit type, because plain byte is already unsigned.
Unsigned types are common in file formats, network protocols, hashing and bit manipulation, where a value is a pattern of bits rather than a quantity that can go negative. They are rarely the right choice for ordinary counters or sizes in application code, because subtracting past zero does not give a negative number.
uint flags = 0xFFu; ulong big = 18_000_000_000_000_000_000UL; ushort port = 8080; sbyte delta = -5;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
sbyte s = -128; // System.SByte, 8 bits, signed ushort u16 = 65535; // System.UInt16, 16 bits uint u32 = 4_294_967_295u; // System.UInt32, 32 bits ulong u64 = 1UL << 63; // System.UInt64, 64 bits
| Keyword | .NET type | Size | Range |
|---|---|---|---|
sbyte |
System.SByte |
8 bits | -128 to 127 |
ushort |
System.UInt16 |
16 bits | 0 to 65,535 |
uint |
System.UInt32 |
32 bits | 0 to 4,294,967,295 |
ulong |
System.UInt64 |
64 bits | 0 to 18,446,744,073,709,551,615 |
| Literal suffix | Type |
|---|---|
u or U |
uint, or ulong if the value does not fit |
l or L |
long (prefer L; lowercase looks like the digit 1) |
ul, UL, lu and so on |
ulong |
There are no suffixes for sbyte and ushort. An integer literal that fits is converted implicitly, so ushort p = 8080; works.
Errors / undefined behavior. Arithmetic that leaves the range wraps around in the default unchecked context and throws OverflowException in a checked context or with the project setting CheckForOverflowUnderflow. Constant expressions that overflow are compile errors. Integer division by zero throws DivideByZeroException.
Usage notes
- Subtraction is the trap.
uint a = 3, b = 5; a - bis4294967294, not-2. A loop likefor (uint i = n - 1; i >= 0; i--)never ends, becausei >= 0is always true. - Mixing signs.
uintcombined withintis promoted tolong, so the result is exact;ulongcombined with a signed type is a compile error because no type holds both ranges. Cast explicitly. - Small types promote. Arithmetic on
sbyteandushortis done inint, soushort a = 1, b = 2; var c = a + b;is anint. Assigning back needs a cast. - Not CLS-compliant.
uint,ulong,ushortandsbyteare not part of the Common Language Specification, so public APIs meant for every .NET language usually avoid them. The BCL itself usesintfor lengths and indexes. - Bit work. Prefer unsigned types for shifts and masks:
>>on an unsigned value always fills with zeros, while on a signed one it keeps the sign bit. The>>>operator gives the same zero-fill for signed types. - Parsing and formatting. Each type has
Parse,TryParse,ToString("X")for hex and the generic-math interfaces such asIBinaryInteger<T>. - Performance. On 64-bit hardware there is no speed difference between signed and unsigned arithmetic of the same width; choose by meaning, not by speed.