MinValue属性或Int16结构的字段用于表示Int16的最小可能值。该字段的值是常数,表示用户无法更改该字段的值。该字段的值为-32768 。其十六进制值为0x8000 。当将数值类型(如Int64和Int32)转换为Int16时,它还将程序从OverflowException中保存下来。
句法:
public const short MinValue = -32768;
返回值:该字段始终返回-32768。
例子:
// C# program to illustrate the
// Int16.MinValue field
using System;
class GFG {
// Main Method
static public void Main()
{
// display the Minimum value of Int16 struct
Console.WriteLine("Minimum Value is: "+
Int16.MinValue);
// Taking an array of long i.e Int64 data type
long []num = {742346, 43443, -345, -7463647};
// 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");
}
}
}
}
输出:
Minimum Value is: -32768
Not Possible
Not Possible
Conversion is Possible.
Not Possible
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.int16.minvalue?view=netframework-4.7.2