📜  如何在C#中获取枚举的哈希码?

📅  最后修改于: 2021-05-29 20:17:42             🧑  作者: Mango

Enum.GetHashCode方法用于获取当前实例值的HashCode。此方法从Object类继承。

句法:

public override int GetHashCode ();

返回:此方法返回32位有符号整数哈希码。

例子:

// C# program to illustrate the
// Enum.GetHashCode() Method
using System;
  
class GFG {
      
    enum Color {Blue, Black};
  
    // Main Method
    public static void Main(String[] args)
    {
        Color c1 = Color.Blue;
        Console.Write("HashCode of Enum Constant " + c1 + " : ");
  
        // Using the GetHashCode() Method
        Console.WriteLine(c1.GetHashCode());
  
        Color c2 = Color.Black;
        Console.Write("Hashcode of Enum Constant " + c2 + " : ");
  
        // Using the GetHashCode Method
        Console.WriteLine(c2.GetHashCode());
    }
}
输出:
HashCode of Enum Constant Blue : 0
Hashcode of Enum Constant Black : 1

参考:

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