Int
In computer programs, a larger, standard type (like int
and uint
) is easier to access than a smaller one (like a bit). In C# int
has advantages: it has optimized performance.
Used in loops, for local variables and class
fields, and returned from methods, the C# int
is everywhere. Int
is 4 bytes, while nint is a native-sized int
.
Int
exampleHere we use ints in a variety of ways, and also test their physical characteristics (memory usage). We use Console.WriteLine
to write the integer to the screen.
int
. It will be placed into a local variable slot in the stack memory.int
type also has MinValue
and MaxValue
properties. These are useful for when you want to loop through all integers.using System; // Part 1: demonstrate an int value type. int number = 11; Console.WriteLine(number); number = -1; // Can be negative Console.WriteLine(number); // Part 2: use operators. Console.WriteLine(number == -1); Console.WriteLine(number + 100); Console.WriteLine(number.GetType()); Console.WriteLine(typeof(int)); // Part 3: get minimum and maximum. Console.WriteLine(int.MinValue); Console.WriteLine(int.MaxValue); // Part 4: find the memory usage for an int in an array. long bytes1 = GC.GetTotalMemory(false); int[] array = new int[1000 * 1000]; array[0] = 1; long bytes2 = GC.GetTotalMemory(false); Console.WriteLine(((bytes2 - bytes1) / (1000 * 1000)).ToString("0 bytes per int"));11 -1 True 99 System.Int32 System.Int32 -2147483648 2147483647 4 bytes per int
Here we consider both Int32
and UInt32
, the unsigned int
type. With int
we can go negative, but with uint
we have a higher maximum.
int.MinValue = -2147483648 int.MaxValue = 2147483647uint.MinValue = 0 uint.MaxValue = 4294967295
Int
argumentsWe use int
as a parameter. You can pass a variable or a constant to the method that uses an integer parameter. An int
argument is a common type.
using System; // Use integer type as argument to method. bool result = IsOdd(1); Console.WriteLine(result); // Test call the method with different integers. result = IsOdd(6); Console.WriteLine(result); result = IsOdd(100); Console.WriteLine(result); result = IsOdd(101); Console.WriteLine(result); // Use the integer parameter in the method body. static bool IsOdd(int number) => number % 2 != 0;True False False True
Uint is similar to int
, but reserves no space for the sign. Unsigned integers can be used through many programs. With uint
we can increase the bounds of a loop.
uint
type is the same as the System.UInt32
type in the System
namespace. This alias provides for clearer code in programs.uint
is executed, it is allocated in the method's stack. No dynamic heap allocation is done. This is efficient.System.Type
object for the 2 numeric types. This shows the struct
that uint
aliases to.using System; // Declare example unsigned and signed integers. uint value1 = 100; int value2 = 100; // Display values. Console.WriteLine(value1); Console.WriteLine(value2); // Assign the maximum values. uint max1 = uint.MaxValue; int max2 = int.MaxValue; // Display maximum values. Console.WriteLine(max1); Console.WriteLine(max2); // Assign the minimum values. uint min1 = uint.MinValue; int min2 = int.MinValue; // Write the minimum values. Console.WriteLine(min1); Console.WriteLine(min2); // Write the types. Console.WriteLine(typeof(uint)); Console.WriteLine(typeof(int));100 100 4294967295 2147483647 0 -2147483648 System.UInt32 System.Int32
Int
errorsIf you try to assign an int
variable in your program to a numeric literal that is too big, the C# compiler will give you a compile-time error.
int
to 3000000000. The C# compiler will give the error here.int
" type to "uint
," your program will compile successfully.class Program { static void Main() { int value1 = 3000000000; } }Error CS0266 Cannot implicitly convert type 'uint' to 'int'. An explicit conversion exists (are you missing a cast?)
int
An int
can never be null
. But we can use a nullable int
, which is a struct
that wraps around an int
value, to represent a null
int
.
int
can be tested directly against null
. This makes it different from a class
instance that has an int
field.using System; // A nullable int can be a number, or null. int? test = 100; Console.WriteLine("Value is {0}", test.Value); test = null; Console.WriteLine("Null: {0}", test == null);Value is 100 Null: True
In C# there is also the concept of a "native" int
. This is the size of integers used in C++ programs based on the computer architecture.
IntPtr
type. When calling a C++ method from a DLL, nint can be a useful syntax feature.IntPtr
, which has the same result and same type as the nint. The code is less elegant.UIntPtr
type, which is unsigned (no negative values are represented).UIntPtr
is the type to which the nuint type is aliased. In some programs, specifying UIntPtr
can be hard to read, while nuint is clearer.uint
, along with the IntPtr
and UIntPtr
types.using System; // Part 1: use an nint. nint test = 10; Console.WriteLine(test); Console.WriteLine(typeof(nint)); // Part 2: use an IntPtr. IntPtr test2 = 10; Console.WriteLine(test2); Console.WriteLine(typeof(IntPtr)); // Part 3: use an nuint. nuint test3 = 1000; Console.WriteLine(test3); Console.WriteLine(typeof(nuint)); // Part 4: use an UIntPtr. UIntPtr test4 = 1000; Console.WriteLine(test4); Console.WriteLine(typeof(UIntPtr)); // Part 5: some maximums. Console.WriteLine(nint.MaxValue); Console.WriteLine(IntPtr.MaxValue); Console.WriteLine(nuint.MaxValue); Console.WriteLine(UIntPtr.MaxValue);10 System.IntPtr 10 System.IntPtr 1000 System.UIntPtr 1000 System.UIntPtr 9223372036854775807 9223372036854775807 18446744073709551615 18446744073709551615
Int
is found in almost every program, while uint
is less common. Int
is exceedingly useful and simple to understand, and can be considered the default number type.