C# Reference ยท Types and Literals
C# nint and nuint native-sized integers
Reference for C# nint and nuint: pointer-sized integers, their size on 32-bit and 64-bit processes, IntPtr relationship, interop use and pitfalls.
nint and nuint are integer types whose size matches the process they run in: 4 bytes in a 32-bit process and 8 bytes in a 64-bit process. nint is signed, nuint is unsigned. They are the language keywords for System.IntPtr and System.UIntPtr (since C# 11 the two names are the same type, so nint is simply IntPtr with full arithmetic).
You need them mostly for interop with native code, where a handle, a pointer-sized offset or a size_t has to be passed as an integer, and for low-level code that mirrors the machine word. Everywhere else, int and long are clearer and portable.
nint handle = 42; nuint size = (nuint)1024; Console.WriteLine(nint.Size); // 4 or 8
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
nint value = 0; // System.IntPtr nuint uvalue = 0; // System.UIntPtr
| Member | Description |
|---|---|
nint.Size, nuint.Size |
Size in bytes: 4 or 8. |
nint.MinValue, nint.MaxValue |
Range of the current process: the int range in 32-bit, the long range in 64-bit. |
nuint.MinValue, nuint.MaxValue |
0 and uint.MaxValue or ulong.MaxValue. |
nint.Zero, nuint.Zero |
The value 0 (inherited from the IntPtr API). |
Conversions. int to nint and nint to long are implicit. Everything else, including long to nint and nint to int, is explicit and may truncate or throw in a checked context.
Errors / undefined behavior. Overflow follows the usual checked/unchecked rules. There are no literal suffixes: write (nint)5 or rely on implicit int constant conversion.
Usage notes
- Same type as
IntPtr. Before C# 9,IntPtrhad almost no operators. Nownintsupports+,-,*,/,%, comparisons, shifts and bitwise operators directly, and you can use either name. - Handles are not numbers. An
nintthat stores an OS handle or an address should not be added to or compared for ordering; wrap it in aSafeHandlewhen you own the resource. - Portable constants are tricky.
nint x = 3_000_000_000;does not compile (the compiler reports CS0266 because the constant is auint), since the constant would not fit in a 32-bitnint. Implicit constant conversions only work for values that fit inint; otherwise cast explicitly. - Serialization. The size changes between processes, so do not write
nintvalues to files or send them over a network. Uselongor a fixed-size type. - Performance. Native-sized integers can avoid conversions when indexing spans or doing pointer arithmetic in
unsafecode, but measure before switching a hot loop; the JIT already widensintindexes efficiently. - Generic math. Both types implement the generic-math interfaces, so they work in constrained generic methods such as
T Sum<T>(IEnumerable<T>) where T : INumber<T>.