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.

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.
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:
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.
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
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
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.
const nint validConst = 2147483647; // Allowed (fits in Int32)
// const nint invalidConst = 2147483648; // Compiler error: Constant value cannot be converted to a 'nint'
Master C# with Deep Grasping Methodology!Learn More