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

# C# nuint

`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` ($2^{32} - 1$)
* **64-bit process:** `0` to `18,446,744,073,709,551,615` ($2^{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 (`+`, `-`, `*`, `/`, `%`, `~`, `&`, `|`, `^`, `<<`, `>>`).

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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`.

```csharp theme={"dark"}
// The compiler treats these as identical signatures, resulting in a compilation error.
public void Process(nuint value) { }
public void Process(UIntPtr value) { } 
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
