When are two enums equal in C#?

狂风中的少年 提交于 2019-11-30 09:25:33

Enums are strongly typed in C#, hence enumA.one != enumB.one. Now, if you were convert each enum to their integer value, they would be equal.

Assert.AreEqual((int)enumA.one, (int)enumB.one);

Also, I'd like to challenge the statement that because they have the same integer or string representation that they should be the same or equals. Given two enumerations NetworkInterface and VehicleType, it would not be logical for C# or the .Net Framework to allow NetworkInterface.None to equal VehicleType.None when compared as enumeration, by either value or string. However, if the developer cast the strongly typed enumeration to an integer or string, there is nothing the language or framework can do to stop the two from being equals.

To further clarify, you cannot override MyEnum.Equals in order to provide a different equality method. .Net enums are not quite the first class citizens they are in later versions of Java, and I wish that C# allowed for richer interactions with Enums.

cpkilekofp

I refer you to the C# Language Specification v3.0, from which this quote has been extracted from the Enum section on page 29:

"Each enum type has a corresponding integral type called the underlying type of the enum type. An enum type that does not explicitly declare an underlying type has an underlying type of int. An enum type’s storage format and range of possible values are determined by its underlying type. The set of values that an enum type can take on is not limited by its enum members. In particular, any value of the underlying type of an enum can be cast to the enum type and is a distinct valid value of that enum type."

The .AreEqual method is really testing equivalence while the second tests identity. So, simply cast each one to its underlying type (in this case, int) and then do the comparison.

public enum enumA { one, two }
public enum enumB { one, two }
[Test]
public void PreTest()
{
        Assert.AreEqual((int)enumA.one,(int)enumB.one);
        Assert.AreSame(enumA.one, enumB.one);
}

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.

If you want them to match, cast them to int

Assert.AreEqual((int)enumA.one,(int)enumB.one);

would pass because they are both the first listed. If you wanted them to match because they both say "one" then you need to use reflection.

To be honest, Equality is not straight forward most of the time.

I would be inclined to create a helper class that implements IEqualityComparer (and any other equality tests, IsSame() for example) and use that.

    public enum enumA {one = 1, two = 2}

    public enum enumB {one = 1, two = 2}

    [Test]
    public void PreTest()
    {                       
        Assert.AreEqual((int)enumA.one, (int)enumB.one);
        // I don't think this one will ever pass
        Assert.AreSame(enumA.one, enumB.one); 
    }

You could try casting them:

Assert.AreEqual((int)enumA.one, (int)enumB.one);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!