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.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version