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

`nint` is a native-sized signed integer type in C# that adapts its memory footprint to match the underlying architecture of the execution environment. It occupies 32 bits (4 bytes) in a 32-bit process and 64 bits (8 bytes) in a 64-bit process.

Introduced in C# 9.0, `nint` (along with its unsigned counterpart, `nuint`) integrates native-sized integers directly into the C# type system as first-class numeric types.

## CLR Representation

At compile time, the C# compiler translates `nint` to the `System.IntPtr` struct. In Intermediate Language (IL), there is no distinction between `nint` and `IntPtr`. However, at the language level, the C# compiler applies different semantic rules to `nint`, treating it as a numeric primitive rather than an opaque pointer type.

```csharp theme={"dark"}
nint nativeValue = 100;
Type t = nativeValue.GetType(); // Evaluates to System.IntPtr
bool isSame = typeof(nint) == typeof(IntPtr); // Evaluates to true
```

## Memory Layout and Bounds

Because the size of `nint` is evaluated at runtime by the CLR, its minimum and maximum values depend on the host architecture:

* **32-bit architecture:** Equivalent to `System.Int32` (`-2,147,483,648` to `2,147,483,647`).
* **64-bit architecture:** Equivalent to `System.Int64` (`-9,223,372,036,854,775,808` to `9,223,372,036,854,775,807`).

You can access these bounds programmatically using the type's static properties:

```csharp theme={"dark"}
nint max = nint.MaxValue;
nint min = nint.MinValue;
```

## Type System and Operations

Unlike `IntPtr` in older versions of C#, `nint` natively supports standard arithmetic, comparison, and bitwise operators without requiring the `unsafe` context or manual casting.

```csharp theme={"dark"}
nint a = 256;
nint b = 128;

nint sum = a + b;        // Arithmetic
nint bitwise = a & b;    // Bitwise operations
bool isGreater = a > b;  // Comparison
a++;                     // Increment/Decrement
```

## Conversions

The C# compiler enforces strict conversion rules for `nint` to prevent overflow exceptions across different architectures.

**Implicit Conversions:**
Types that are guaranteed to fit within a 32-bit integer can be implicitly converted to `nint`.

* `sbyte`, `byte`, `short`, `ushort`, `int`

```csharp theme={"dark"}
int standardInt = 42;
nint nativeInt = standardInt; // Implicit conversion
```

**Explicit Conversions:**
Types that exceed 32 bits, or types that `nint` might exceed on a 64-bit system, require explicit casting.

* To `nint`: `uint`, `long`, `ulong`, `nuint`, `float`, `double`, `decimal`
* From `nint`: `sbyte`, `byte`, `short`, `ushort`, `int`, `uint`, `long`, `ulong`, `nuint`

```csharp theme={"dark"}
long largeValue = 5000000000;
nint castedNative = (nint)largeValue; // Explicit cast required

nint nativeVal = 100;
long castedLong = (long)nativeVal;    // Explicit cast required
```

## Compile-Time Constants

You can declare `nint` as a `const`, but the assigned value must be resolvable at compile time and must fit within the bounds of a 32-bit signed integer (`Int32`). The compiler enforces this restriction because it cannot guarantee that a 64-bit value will be valid on the target machine at runtime.

```csharp theme={"dark"}
const nint validConst = 2147483647; // Allowed (fits in Int32)
// const nint invalidConst = 2147483648; // Compiler error: Constant value cannot be converted to a 'nint'
```

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