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

📅  最后修改于: 2021-05-30 01:39:30             🧑  作者: Mango

UInt16 Struct的MaxValue字段用于表示16位无符号整数的最大值。该字段的值是常数,表示用户无法更改该字段的值。该字段的值为65535。其十六进制值为0xFFFF 。如果整数值不在UInt16类型的范围内,则用于避免OverflowException。

句法:

public const ushort MaxValue = 65535;

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

例子:

// C# program to illustrate the
// UInt16.MaxValue field
using System;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
        // display the Maximum value
        // of UInt16 struct
        Console.WriteLine("Maximum Value is: " + UInt16.MaxValue);
  
        // Taking an array of the 
        // unsigned long integer 
        // i.e UInt64 data type
        ulong[] num = {3422146, 7443, 43535776, 2342534654756123};
  
        // taking variable of UInt16 type
        ushort mynum;
  
        foreach(long 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");
            }
        }
    }
}

输出:

Maximum Value is: 65535
Not Possible
Conversion is Possible.
Not Possible
Not Possible

参考:

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