Compartilhar via


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 variable of the type uint like this example:

uint myUint = 4294967290;

When an integer literal has no suffix, its type is the first of these types in which its value can be represented: int, uint, long, ulong. In this example, it is uint:

uint uInt1 = 123;

You can also use the suffix u or U, such as this:

uint uInt2 = 123U;

When you use the suffix U or u, the type of the literal is determined to be either uint or ulong according to the numeric value of the literal. For example:

Console.WriteLine(44U.GetType());
Console.WriteLine(323442434344U.GetType());

This code displays System.UInt32, followed by System.UInt64 -- the underlying types for uint and ulong respectively -- because the second literal is too large to be stored by the uint type.

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# Reference).

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

Reference

C# Keywords

Integral Types Table (C# Reference)

Built-In Types Table (C# Reference)

Implicit Numeric Conversions Table (C# Reference)

Explicit Numeric Conversions Table (C# Reference)

UInt32

Concepts

C# Programming Guide

Other Resources

C# Reference