Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The uint keyword in C# denotes an integral numeric value type that stores 32-bit unsigned integers. It serves as a language alias for the .NET System.UInt32 struct. Because it is unsigned, it allocates all 32 bits to represent the magnitude of the number, meaning the Most Significant Bit (MSB) is not reserved as a sign bit.

Technical Specifications

  • Memory Size: 32 bits (4 bytes)
  • Value Range: 0 to 4,294,967,295 (UInt32.MinValue to UInt32.MaxValue)
  • Default Value: 0
  • Underlying Type: System.UInt32
  • CLS Compliance: Not CLS-compliant
Architectural Note on CLS Compliance: Because uint is not part of the Common Language Specification (CLS), exposing it in public APIs can compromise interoperability. Libraries that expose uint in public methods or properties may not be fully consumable by all .NET languages.

Syntax and Initialization

Integer literals without a suffix are automatically typed as uint by the compiler if their value falls between 2147483648 and 4294967295. The u or U suffix is used to explicitly type a literal as an unsigned integer. Its primary purpose is to force a value within the standard int range to be evaluated as a uint. This is required for implicit typing or for resolving specific method overloads where the compiler would otherwise default to int.
// Automatic uint typing for values exceeding int.MaxValue
uint maxUnsigned = 4294967295;

// Suffix forces uint type on a value within the standard int range
var count = 5U; // Type is implicitly evaluated as uint

// Hexadecimal literal assignment
uint hexValue = 0xFFFFFFFF;

// Binary literal assignment with digit separators
uint binValue = 0b_1111_1111_1111_1111_1111_1111_1111_1111;

Type Conversions

The compiler handles conversions to and from uint based on the target type’s capacity to safely store a 32-bit unsigned value without data loss. Implicit Conversions A uint can be implicitly converted to long, ulong, float, double, or decimal.
uint baseValue = 2147483648;

long implicitLong = baseValue;
float implicitFloat = baseValue;
Explicit Conversions Explicit casting is required when converting a uint to a signed int, or to smaller integral types (short, ushort, byte, sbyte). Explicit casting is also required when converting from signed types (like int) to uint.
int signedInt = -1;

// Explicit cast required. 
// In an unchecked context, this results in bitwise reinterpretation (underflow).
// -1 becomes 4294967295.
uint castFromInt = (uint)signedInt; 

long largeLong = 5000000000L;

// Explicit cast required.
// Results in truncation of the high-order bits.
uint castFromLong = (uint)largeLong; 

Overflow and Underflow Behavior

By default, arithmetic operations on uint execute in an unchecked context. If an operation exceeds UInt32.MaxValue or drops below UInt32.MinValue, the value wraps around. When performing arithmetic with literals, C#‘s binary numeric promotion rules dictate that applying a binary operator to a uint and a standard int literal (like 1) promotes both operands to long. Assigning the resulting long back to a uint without a cast causes Compiler Error CS0266. To maintain the operation strictly within the uint context, the literal must be suffixed with U.
uint max = uint.MaxValue;
uint overflowResult = max + 1U; // Evaluates to 0

uint min = uint.MinValue;
uint underflowResult = min - 1U; // Evaluates to 4294967295
To enforce an OverflowException instead of wrapping, the operation must be wrapped in a checked block or the compiler flag must be set.
checked
{
    uint max = uint.MaxValue;
    // Throws System.OverflowException
    uint overflowResult = max + 1U; 
}
Master C# with Deep Grasping Methodology!Learn More