When are two enums equal in C#?

前端 未结 7 1787
栀梦
栀梦 2020-12-31 22:39

I have created two enums and I know they are not the same but still I think it makes sense they would be equal since their string represent

7条回答
  •  再見小時候
    2020-12-31 23:18

    Unlike Java, C# does not provide any facility for adding methods (such as operator==()) to an enum.

    What I have done in the past when needing smarter enums is create an XHelper class (where X is the name of the enum), and I put all of the methods on it. Thus something like this:

    public static bool EnumAHelper.EqualsEnumB(EnumA enumA, EnumB enumB)
    {
        return (int)enumA == (int)enumB;
    }
    

    Though, I do not recall running into a case where I needed two different enums to signify the same thing.

提交回复
热议问题