Int16.MaxValue Field in C# with Examples
The MaxValue field or property of Int16 Struct is used to represent the maximum value of Int16. The value of this field is constant means that the user cannot change the value of this field. The value of this field is 32767. Its hexadecimal value is 0x7FFF. It is used to avoid the OverflowException while converting from a numeric type with a greater upper range (such as a UInt16 or an Int32) to an Int16.
Syntax:
public const short MaxValue = 32767;
Return Value: This field always returns 32767.
Example:
// C# program to illustrate the // Int16.MaxValue field using System; class GFG { // Main Method static public void Main() { // display the Maximum value // of Int16 struct Console.WriteLine( "Maximum Value is: " + Int16.MaxValue); // Taking an array of long i.e Int64 data type long []num = {345, -4677, -65245465445441, 44456564}; // taking variable of Int16 type short mynum; foreach ( long n in num){ if (n >= Int16.MinValue && n <= Int16.MaxValue) { // using the method of Convert class // to convert Int64 to Int16 mynum = Convert.ToInt16(n); Console.WriteLine( "Conversion is Possible." ); } else { Console.WriteLine( "Not Possible" ); } } } } |
Output:
Maximum Value is: 32767 Conversion is Possible. Conversion is Possible. Not Possible Not Possible
Reference:
Please Login to comment...