Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What the difference between nint vs int?

Tags:

c#

integer

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?

like image 649
Volodymyr Osadchuk Avatar asked Oct 18 '25 06:10

Volodymyr Osadchuk


1 Answers

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.

like image 160
Fede Avatar answered Oct 20 '25 20:10

Fede