📜  C#| UInt64结构

📅  最后修改于: 2021-05-30 00:48:54             🧑  作者: Mango

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

领域

Field Description
MaxValue Represents the largest possible value of UInt64. This field is constant.
MinValue Represents the smallest possible value of UInt64. This field is constant.

例子:

// 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!

方法

Method Description
CompareTo() Compares the current instance to a specified object or UInt64 and returns an indication of their relative values.
Equals() Returns a value which shows whether the current instance is equal to a specified object or UInt64.
GetHashCode() Returns the hash code for the current instance.
GetTypeCode() Returns the TypeCode for value type UInt64.
Parse() Converts the string representation of a number to its 64-bit unsigned integer equivalent.
ToString() Converts the numeric value of the current instance to its equivalent string representation.
TryParse() Tries to convert the string representation of a number to its 64-bit unsigned integer equivalent. A return value indicates whether the conversion succeeded or failed.

例子:

// 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/zh-cn/dotnet/api/system.uint64?view=netframework-4.8