Search code examples
c#integer

What the difference between nint vs int?


I'm curious about the introduction of nint for native-sized integers in C# 9. Could someone provide a detailed comparison between int and nint, focusing on their practical differences in usage and behavior?

int num1;
nint num2;

I'm particularly interested in understanding when it's appropriate to use nint?


Solution

  • nint and nuint are new keywords to represent value types that map to the already existing System.IntPtr and System.UIntPtr.

    These types are commonly used in interop scenarios, as their size depends on the runtime platform, that is they are 32 bits on 32 bit systems, and 64 bits on 64 bit systems. More info here.

    On the other hand, int maps to the System.Int32 value type, which is always a 32 bit value, no matter the runtime platform.