Int16.MinValue 字段

定义

表示 Int16 的最小可能值。 此字段为常数。

C#
public const short MinValue = -32768;

字段值

Value = -32768

示例

下面的示例使用 MinValue 属性在转换为Int16值时阻止 OverflowException

C#
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 {newNumber} to an Int16.");
   }
   else
   {
      Console.WriteLine($"Unable to convert {number} to an Int16.");
   }
}
// The example displays the following output to the console:
//       Unable to convert 162345 to an Int16.
//       Successfully converted 32183 to an Int16.
//       Unable to convert -54000 to an Int16.

注解

此常量的值为 -32768;即十六进制0x8000。

属性 MinValue 通常用于防止 OverflowException 从具有较大较小范围的数字类型转换为 (如 Int32Int64) 到 Int16。 该示例演示了此用法。

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

另请参阅