C# int.MaxValue, MinValue (Get Lowest Number)Examine the int.MaxValue and int.MinValue constants. Use int.MaxValue to find the lowest number.
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 (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.
Microsoft states that MaxValue is a "public const int". With a bit of exploration, I found this information about the MaxValue and MinValue constants.
Fields: The fields are constant values and can be accessed anywhere with the composite name.
Tip: By using these constants, you can avoid typing out 10-digit numbers in your programs.const
One problem I have dealt with is keeping track of the lowest number found. You can use int.MaxValue to start the value high, and then any lower number will be valid.Foreach
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.
C# program that uses int.MaxValue
static void Main()
int integerArray = new int
// 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;
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.
C# program that uses MinValue and MaxValue
static void Main()
// Display min and max.
Console.WriteLine("MIN: " + int.MinValue);
Console.WriteLine("MAX: " + int.MaxValue);
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: Prefer ushort, short and byte, for data structures—this reduces memory use.short, ushortByte
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?".Nullable
A summary. We saw values and examples for the max and min constants on number types in the C# language. I suggest that you avoid typing out numbers like -2,147,483,648 unless needed.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to firstname.lastname@example.org.