C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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.
constConstant values:
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
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
using System;
class Program
{
static void Main()
{
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);
}
}
}
}
Output
10000
600
1
C# program that uses MinValue and MaxValue
using System;
class Program
{
static void Main()
{
// Display min and max.
Console.WriteLine("MIN: " + int.MinValue);
Console.WriteLine("MAX: " + int.MaxValue);
}
}
Output
MIN: -2147483648
MAX: 2147483647
But: Prefer ushort, short and byte, for data structures—this reduces memory use.
short, ushortByteAlso: Instead of using int.MaxValue and int.MinValue, consider nullable types, used with Nullable<int> or "int?".
Nullable