C# UInt64结构详细用法介绍

2021年5月7日13:46:32 发表评论 690 次浏览

C#中, UInt64结构用于表示64位无符号整数(也称为乌龙数据类型), 范围从0到18, 446, 744, 073, 709, 551, 615。它还提供了不同类型的方法来比较此类型的实例, 将实例的值转换为其String表示形式, 将数字的String表示形式转换为该类型的实例等。此结构在系统命名空间。 UInt64结构继承了ValueType类, 该类继承了对象类别.

领域

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

例子:

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

输出如下:

Equal to MaxValue!

方法

方法 描述
CompareTo() 将当前实例与指定的对象或UInt64比较, 并返回其相对值的指示。
Equals() 返回一个值, 该值显示当前实例是否等于指定的对象或UInt64。

GetHashCode() 返回当前实例的哈希码。
GetTypeCode() 返回值类型UInt64的TypeCode。
Parse() 将数字的字符串表示形式转换为其等效的64位无符号整数。
ToString() 将当前实例的数值转换为其等效的字符串表示形式。
TryParse() 尝试将数字的字符串表示形式转换为其等效的64位无符号整数。返回值指示转换是成功还是失败。

例子:

//C# program to illustrate how to get the 
//hash code of the 64-bit Unsigned integer
using System;
  
class GFG {
  
     //Main Method
     static public void Main()
     {
  
         //UInt64 variable
         ulong myval = 3654121225155;
  
         //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: -895944559

参考:

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

木子山

发表评论

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