Day 6: C# Integral Numeric TypesDay 6: C# Integral Numeric Types
0 0
Read Time:2 Minute, 9 Second
sbyte	-128 to 127	Signed 8-bit integer

byte	0 to 255	Unsigned 8-bit integer

short	-32,768 to 32,767	Signed 16-bit integer

ushort	0 to 65,535	Unsigned 16-bit integer

int	-2,147,483,648 to 2,147,483,647	Signed 32-bit integer

uint	0 to 4,294,967,295	Unsigned 32-bit integer

long	-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807	Signed 64-bit integer

ulong	0 to 18,446,744,073,709,551,615	Unsigned 64-bit integer

nint	Depends on platform (computed at runtime)	Signed 32-bit or 64-bit integer

nuint	Depends on platform (computed at runtime)	Unsigned 32-bit or 64-bit integer

int & .NET Type: System.Int32

int a = 123;
System.Int32 b = 123;

The nint and nuint types in the last two rows of the table are native-sized integers. Starting in C# 9.0, you can use the nint and nuint keywords to define native-sized integers. These are 32-bit integers when running in a 32-bit process, or 64-bit integers when running in a 64-bit process. They can be used for interop scenarios, low-level libraries, and to optimize performance in scenarios where integer math is used extensively.

Integer literals

Integer literals can be

  • decimal: without any prefix
  • hexadecimal: with the 0x or 0X prefix
  • binary: with the 0b or 0B prefix
var decimalLiteral = 42;
var hexLiteral = 0x2A;
var binaryLiteral = 0b_0010_1010;

the use of _ as a digit separator, which is supported starting with C# 7.0. You can use the digit separator with all kinds of numeric literals.

To get the size of a native-sized integer at run time, you can use sizeof()

Console.WriteLine($"size of nint = {sizeof(nint)}");
Console.WriteLine($"size of nuint = {sizeof(nuint)}");

// output when run in a 64-bit process
//size of nint = 8
//size of nuint = 8

// output when run in a 32-bit process
//size of nint = 4
//size of nuint = 4

To get the minimum and maximum values of native-sized integers at run time, use MinValue and MaxValue

Console.WriteLine($"nint.MinValue = {nint.MinValue}");
Console.WriteLine($"nint.MaxValue = {nint.MaxValue}");
Console.WriteLine($"nuint.MinValue = {nuint.MinValue}");
Console.WriteLine($"nuint.MaxValue = {nuint.MaxValue}");

// output when run in a 64-bit process
//nint.MinValue = -9223372036854775808
//nint.MaxValue = 9223372036854775807
//nuint.MinValue = 0
//nuint.MaxValue = 18446744073709551615

// output when run in a 32-bit process
//nint.MinValue = -2147483648
//nint.MaxValue = 2147483647
//nuint.MinValue = 0
//nuint.MaxValue = 4294967295
Console.WriteLine("Minimum Value is: "+
                               Int32.MinValue);

Thank You

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

About Author

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Reply

Your email address will not be published. Required fields are marked *