📜  C#中的Decimal.GetHashCode方法与示例

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

Decimal.GetHashCode方法用于获取当前Decimal实例的HashCode。

下面的程序说明了上面讨论的方法的使用:

范例1:

// C# program to illustrate the
// Decimal.GetHashCode() Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // Taking decimal variables
        // having fractional part
        Decimal dec1 = 214748.78549M;
  
        // Getting the hash code for Decimal
        // using GetHashCode() method
        int result = dec1.GetHashCode();
  
        // Display the time
        Console.WriteLine("HashCode for Decimal is: {0}",
                                                 result);
    }
}
输出:
HashCode for Decimal is: 161795526

范例2:

// C# program to illustrate the
// Decimal.GetHashCode() Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // using result() Method
        result(Decimal.MinValue);
        result(Decimal.MaxValue);
    }
  
    // result() method
    public static void result(decimal val)
    {
  
        // using GetHashCode() method
        int code = val.GetHashCode();
  
        // Display the hashcode
        Console.WriteLine("HashCode for {0} is {1}",
                                         val, code);
    }
}
输出:
HashCode for -79228162514264337593543950335 is -974127104
HashCode for 79228162514264337593543950335 is 1173356544

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.decimal.gethashcode?view=netframework-4.7.2