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.

nuint is a native-sized unsigned integer type introduced in C# 9.0. Its memory footprint and value range are platform-dependent, resolving to 32 bits (4 bytes) in a 32-bit execution context and 64 bits (8 bytes) in a 64-bit execution context. It serves as the C# equivalent of the C/C++ size_t type, making it a critical primitive for P/Invoke, unmanaged interoperability, and representing memory sizes or indices.

Type System Mapping

At the compiler level, the nuint keyword is an alias for the .NET System.UIntPtr struct. However, unlike the legacy UIntPtr type in older versions of C#, nuint is treated by the compiler as a primitive numeric type. This enables direct support for arithmetic, bitwise, and comparison operations without requiring manual method calls.

Value Range

Because the size is determined at runtime by the host architecture, the maximum value of nuint fluctuates:
  • 32-bit process: 0 to 4,294,967,295 (23212^{32} - 1)
  • 64-bit process: 0 to 18,446,744,073,709,551,615 (26412^{64} - 1)
These boundaries can be accessed programmatically using nuint.MinValue and nuint.MaxValue. Because the size of nuint depends on the runtime architecture, these boundaries are implemented as static readonly fields (in C# 9) or static properties (in .NET 7+). They are not compile-time constants (const). Consequently, they cannot be used in constant expressions, such as default parameter values, attribute arguments, or switch cases.

Syntax and Operations

nuint supports standard mathematical and bitwise operators (+, -, *, /, %, ~, &, |, ^, <<, >>).
nuint val1 = 42;
nuint val2 = 100;

// Arithmetic operations
nuint sum = val1 + val2;
nuint product = val1 * val2;

// Bitwise operations
nuint bitwiseAnd = val1 & val2;
nuint bitShift = val1 << 2;

Type Conversions

The compiler enforces strict conversion rules based on the guarantee that nuint is at least 32 bits wide and at most 64 bits wide. Implicit Conversions: Unsigned types that are 32 bits or smaller can be implicitly converted to nuint because they are guaranteed to fit within the type’s memory space regardless of the architecture. Conversely, nuint can be implicitly converted to ulong. Because nuint maxes out at 64 bits, it is guaranteed to fit inside a 64-bit ulong without data loss.
byte b = 10;
ushort us = 20;
uint ui = 30;

// Implicit conversions TO nuint
nuint n1 = b;  
nuint n2 = us; 
nuint n3 = ui; 

// Implicit conversion FROM nuint
ulong ul = n3; // Valid: nuint is at most 64 bits
Explicit Conversions: Converting a 64-bit ulong to nuint, or converting any signed type to nuint, requires an explicit cast. A 64-bit integer might overflow a 32-bit nuint at runtime, and signed integers violate the unsigned constraint.
ulong largeVal = 40;
int i = -50;
nint ni = -60; // Native-sized signed integer

nuint n4 = (nuint)largeVal; // Requires cast (nuint might be 32-bit at runtime)
nuint n5 = (nuint)i;        // Requires cast (sign difference)
nuint n6 = (nuint)ni;       // Requires cast (sign difference)

Pointer Interoperability

nuint is deeply integrated with C#‘s unsafe pointer types. It can be explicitly cast to and from any unmanaged pointer type, representing the exact memory address. As the equivalent of size_t, it is the standard type for passing pointer sizes, buffer lengths, and memory block indices to native APIs via P/Invoke.
unsafe
{
    int value = 1024;
    int* ptr = &value;

    // Cast pointer to native unsigned integer
    nuint address = (nuint)ptr;

    // Cast native unsigned integer back to pointer
    int* restoredPtr = (int*)address;
}

Reflection and Overloading

Because nuint is a language-level projection of System.UIntPtr, they are indistinguishable via reflection at runtime. Consequently, method overloading cannot differentiate between nuint and UIntPtr.
// The compiler treats these as identical signatures, resulting in a compilation error.
public void Process(nuint value) { }
public void Process(UIntPtr value) { } 
Master C# with Deep Grasping Methodology!Learn More