C# UInt16结构用法详细介绍

2021年5月7日13:48:57 发表评论 1,305 次浏览

C#中, UInt16结构用于表示16位无符号整数(也称为超短数据类型)从范围0至65535。它提供了不同类型的方法来执行各种操作, 例如比较此类型的实例, 将实例的值转换为其字符串表示形式, 将数字的字符串表示形式转换为该类型的实例等。此结构在系统命名空间。 UInt16结构继承了ValueType类, 该类继承了对象类别.

领域

领域 描述
最大值 表示UInt16的最大可能值。该字段是恒定的。
最小值 表示UInt16的最小可能值。该字段是恒定的。

例子:

//C# program to illustrate the 
//fields of UInt16 struct
using System;
  
class GFG {
  
     //Main Method
     static public void Main()
     {
  
         //Unsigned 16-bit integer
         ushort val = 295;
  
         //Checking the unsigned integer
         if (val.Equals(UInt16.MinValue)) 
         {
             Console.WriteLine( "Equal to MinValue..!" );
         }
  
         else if (val.Equals(UInt16.MaxValue)) 
         {
             Console.WriteLine( "Equal to MaxValue" );
         }
  
         else 
         {
             Console.WriteLine( "Not equal" );
         }
     }
}

输出如下:

Not equal

方法

方法 描述
CompareTo() 将当前实例与指定的对象或UInt16比较, 并返回其相对值的指示。
Equals() 返回一个值, 该值显示当前实例是否等于指定的对象或UInt16。
GetHashCode() 返回此实例的哈希码。
GetTypeCode() 返回值类型UInt16的TypeCode。
Parse() 将数字的字符串表示形式转换为其等效的16位无符号整数。
ToString() 将此实例的数值转换为其等效的字符串表示形式。
TryParse() 将数字的字符串表示形式转换为其等效的16位无符号整数。返回值指示转换是成功还是失败。

例子:

//C# program to illustrate how to get the hash 
//code of the 16-bit Unsigned integer
using System;
  
class GFG {
  
     //Main Method
     static public void Main()
     {
  
         //UInt16 variable
         ulong myval = 545;
  
         //Get the hash code
         //Using GetHashCode Method
         int res = myval.GetHashCode();
  
         Console.WriteLine( "The hash code of myval is: {0}" , res);
     }
}

输出如下:

The hash code of myval is: 545

参考:

  • https://docs.microsoft.com/en-us/dotnet/api/system.uint16?view=netframework-4.8

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: