Introduction

First what are native-sized integers ? They are designed to be a integer whose size is specific to the platform. In other words, an instance of this type must be 32 bits on 32-bit and 64-bit hardware and operating systems on 64-bit hardware and operating systems.

The CLR / JIT / MSIL supports the definition and usage of native integers / unsigned integers. Since .NET 4.0’s CLR, it is possible to add / substract an integer from a System.IntPtr System.UIntPtr, and it is possible to do == / != comparisons with other System.IntPtr System.UIntPtr, but any other comparison operation is prohibited…, i.e. they cannot be compared with >>= etc. to each other, so System.IntPtrSystem.UIntPtr remain very basic in the amount of pointer arithmetic.

C## 9 brings what mono has brought before: language support for a native-sized signed and unsigned integer types with nintand nuint keyword. The motivation here is for interop scenarios and for low-level libraries, so might not use them often.

Types nint and nuint are represented by the underlying types System.IntPtr and System.UIntPtr with compiler surfacing additional conversions and operations for those types as native ints.

Sources: Microsoft and Github

C## 9 syntax, constants and usage

nint constants are in the range [ int.MinValueint.MaxValue ].

nuint constants are in the range [ uint.MinValueuint.MaxValue ].

There are no MinValue or MaxValue o nint or nuint because, other than nuint.MinValue, those values cannot be emitted as constants.

#c# #.net 5 #c# 9 #nuint #system.intptr #csharp

Introducing C# 9: Native-sized integers
8.15 GEEK