如何在 C# 中获取枚举的 hashcode?

原文:https://www . geesforgeks . org/how-to-get-hashcode-for-enum-in-c-sharp/

枚举。GetHashCode 方法用于获取当前实例的值的 HashCode。该方法继承自对象类

语法:

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

参考: