📜  C#中的UInt16.MinValue字段(带示例)

📅  最后修改于: 2021-05-29 14:29:08             🧑  作者: Mango

UInt16 Struct的MinValue字段用于表示16位无符号整数(即ushort数据类型)的最小可能值。该字段的值是常数,表示用户无法更改该字段的值。该字段的值为0 。如果该整数值不在UInt16类型的范围内,则它还会从OverflowException中保存程序。 UInt16字段的主要用途是在将Int32值转换为UInt16之前,检查Int32值是否在UInt16类型的范围内。

句法:

public const ushort MinValue = 0;

返回值:该字段始终返回0。

例子:

// C# program to illustrate the
// UInt16.MinValue field
using System;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
        // display the Minimum value of UInt16 struct
        Console.WriteLine("Minimum Value is: " + UInt16.MinValue);
  
        // Taking an array of the 
        // unsigned long integer 
        // i.e UInt64 data type
        ulong[] num = {232146, 14343, 23122345, 4576863645437};
  
        // taking variable of UInt16 type
        ushort mynum;
  
        foreach(ulong n in num)
        {
  
            if (n >= UInt16.MinValue && n <= UInt16.MaxValue) {
  
                // using the method of Convert class
                // to convert Int64 to UInt16
                mynum = Convert.ToUInt16(n);
  
                Console.WriteLine("Conversion is Possible.");
            }
  
            else
            {
                Console.WriteLine("Not Possible");
            }
        }
    }
}

输出:

Minimum Value is: 0
Not Possible
Conversion is Possible.
Not Possible
Not Possible

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.uint16.minvalue?view=netstandard-2.1