Int16结构的MaxValue字段或属性用于表示Int16的最大值。该字段的值是常数,表示用户无法更改该字段的值。该字段的值为32767。其十六进制值为0x7FFF 。从较大范围的数字类型(例如UInt16或Int32 )转换为Int16时,可以使用它来避免OverflowException 。
句法:
public const short MaxValue = 32767;
返回值:该字段始终返回32767。
例子:
// 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");
}
}
}
}
输出:
Maximum Value is: 32767
Conversion is Possible.
Conversion is Possible.
Not Possible
Not Possible
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.int16.maxvalue?view=netframework-4.7.2