int.MaxValue and int.MinValue
This page was last reviewed on May 11, 2023.
Dot Net Perls
Int.MaxValue. In C# an int has a maximum value it can represent. This value is found with int.MaxValue. The minimum value too can be determined with int.MinValue.
int, uint
Numeric types, like int, uint, short and ushort, have specific max values. These are constants—they never change. But we do not need to memorize them to use them.
MaxValue and MinValue are constant values. They can be accessed anywhere with their composite names. These consts make programs easier to understand.
short.MaxValue: 32767 short.MinValue: -32768 ushort.MaxValue: 65535 ushort.MinValue: 0 int.MaxValue: 2,147,483,647 int.MinValue: -2,147,483,648 uint.MaxValue: 4,294,967,295 uint.MinValue: 0 long.MaxValue: 9,223,372,036,854,775,807 long.MinValue: -9,223,372,036,854,775,808
First example. Often we must keep track of the lowest number found. We can use int.MaxValue to start the value high, and then any lower number will be valid.
Note When you start your variable at MaxValue, you will want to know the constraints in your program.
Important If the MaxValue can occur, then you will need to be careful with the logic. But it would work correctly here.
using System; int[] integerArray = new int[] { 10000, 600, 1, 5, 7, 3, 1492 }; // This will track the lowest number found int lowestFound = int.MaxValue; foreach (int i in integerArray) { // By using int.MaxValue as the initial value, this check will usually succeed. if (lowestFound > i) { lowestFound = i; Console.WriteLine(lowestFound); } }
10000 600 1
Min, max. We can access MinValue and MaxValue in the same way. Be careful not to assign an unsigned value to int.MinValue—it is a negative number.
using System; // Display min and max. Console.WriteLine("MIN: " + int.MinValue); Console.WriteLine("MAX: " + int.MaxValue);
MIN: -2147483648 MAX: 2147483647
Discussion. In C# and Windows, there is no performance boost in using smaller data types in loops. Using ushort instead of int just creates unclear code.
But It is a good plan to prefer ushort, short and byte, for data structures—this reduces memory use.
short, ushort
byte, sbyte
Constants. When you have other constants to define, declare them as "public const int ValueName". This has great performance, and is standard and clear. It works well with IntelliSense.
Also Instead of using int.MaxValue and int.MinValue, consider nullable types, used with Nullable int or "int?".
The max and min constants on number types (like int) are often useful in C# programs. We should avoid typing out numbers like -2,147,483,648 unless needed.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on May 11, 2023 (edit).
© 2007-2024 Sam Allen.