C# Int16 MaxValue
Description
Int16 MaxValue
represents the largest possible value
of an Int16. This field is constant.
Syntax
Int16.MaxValue
has the following syntax.
public const short MaxValue
Example
The following example uses the MaxValue property to prevent an OverflowException when converting to an Int16 value.
//from ww w.j a v a 2s .c om
using System;
public class MainClass{
public static void Main(String[] argv){
long[] numbersToConvert = {162345, 32183, -54000};
short newNumber;
foreach (long number in numbersToConvert)
{
if (number >= Int16.MinValue && number <= Int16.MaxValue)
{
newNumber = Convert.ToInt16(number);
Console.WriteLine("Successfully converted {0} to an Int16.",
newNumber);
}
else
{
Console.WriteLine("Unable to convert {0} to an Int16.", number);
}
}
}
}
The code above generates the following result.