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.
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.
// Display min and max.
Console.WriteLine("MIN: " + int.MinValue);
Console.WriteLine("MAX: " + int.MaxValue);MIN: -2147483648
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.