uint (C# Reference)

The uint keyword signifies an integral type that stores values according to the size and range shown in the following table.

Type Range Size .NET Framework type
uint 0 to 4,294,967,295 Unsigned 32-bit integer System.UInt32

Note The uint type is not CLS-compliant. Use int whenever possible.

Literals

You can declare and initialize a uint variable by assigning a decimal literal, a hexadecimal literal, or (starting with C# 7) a binary literal to it. If the integer literal is outside the range of uint (that is, if it is less than System.UInt32.MinValue or greater than System.UInt32.MaxValue), a compilation error occurs.

In the following example, integers equal to 3,000,000,000 that are represented as decimal, hexadecimal, and binary literals are assigned to uint values.

uint uintValue1 = 3000000000;
Console.WriteLine(uintValue1);

uint uintValue2 = 0xB2D05E00;
Console.WriteLine(uintValue2);

uint uintValue3 = 0b1011_0010_1101_0000_0101_1110_0000_0000;
Console.WriteLine(uintValue3);
// The example displays the following output:
//          3000000000
//          3000000000
//          3000000000
Note

You use the prefix 0x or 0X to denote a hexadecimal literal and the prefix 0b or 0B to denote a binary literal. Decimal literals have no prefix.

Starting with C# 7, you can also use the underscore character, _, as a digit separator to enhance readability, as the following example shows.

uint uintValue1 = 3_000_000_000;
Console.WriteLine(uintValue1);

uint uintValue2 = 0xB2D0_5E00;
Console.WriteLine(uintValue2);

uint uintValue3 = 0b1011_0010_1101_0000_0101_1110_0000_0000;
Console.WriteLine(uintValue3);
// The example displays the following output:
//          3000000000
//          3000000000
//          3000000000

Integer literals can also include a suffix that denotes the type. The suffix U or 'u' denotes either a uint or a ulong, depending on the numeric value of the literal. The following example uses the u suffix to denote an unsigned integer of both types. Note that the first literal is a uint because its value is less than System.UInt32.MaxValue, while the second is a ulong because its value is greater than System.UInt32.MaxValue.

object value1 = 4000000000u;
Console.WriteLine($"{value1} ({4000000000u.GetType().Name})");
object value2 = 6000000000u;
Console.WriteLine($"{value2} ({6000000000u.GetType().Name})");

If an integer literal has no suffix, its type is the first of the following types in which its value can be represented:

  1. int
  2. uint
  3. long
  4. ulong

Conversions

There is a predefined implicit conversion from uint to long, ulong, float, double, or decimal. For example:

float myFloat = 4294967290;   // OK: implicit conversion to float  

There is a predefined implicit conversion from byte, ushort, or char to uint. Otherwise you must use a cast. For example, the following assignment statement will produce a compilation error without a cast:

long aLong = 22;  
// Error -- no implicit conversion from long:  
uint uInt1 = aLong;   
// OK -- explicit conversion:  
uint uInt2 = (uint)aLong;  

Notice also that there is no implicit conversion from floating-point types to uint. For example, the following statement generates a compiler error unless an explicit cast is used:

// Error -- no implicit conversion from double:  
uint x = 3.0;  
// OK -- explicit conversion:  
uint y = (uint)3.0;   

For information about arithmetic expressions with mixed floating-point types and integral types, see float and double.

For more information about implicit numeric conversion rules, see the Implicit Numeric Conversions Table.

C# Language Specification

For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.

See Also

UInt32
C# Reference
C# Programming Guide
C# Keywords
Integral Types Table
Built-In Types Table
Implicit Numeric Conversions Table
Explicit Numeric Conversions Table