When are two enums equal in C#?

2019-02-07 03:24发布

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 representation as well as their numeral representation are equal (and even the same...).

In other words : I would like the first test to pass and the second one to fail. In reality however, they both fail. So : when are two enums in C# equal? Or is there anyway to define the equals operator in C#?

Thanks!

    public enum enumA {one, two}

    public enum enumB {one, two}

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

UPDATE : 1) So the answers so far all compare representations, be it ints or strings. The enum itself is always unequal I gather? No means to define equality for it?

7条回答
Explosion°爆炸
2楼-- · 2019-02-07 03:54

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.

查看更多
再贱就再见
3楼-- · 2019-02-07 04:00

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.

查看更多
Anthone
4楼-- · 2019-02-07 04:07

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.

查看更多
时光不老,我们不散
5楼-- · 2019-02-07 04:13
    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); 
    }
查看更多
可以哭但决不认输i
6楼-- · 2019-02-07 04:16

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);
}
查看更多
孤傲高冷的网名
7楼-- · 2019-02-07 04:16

You could try casting them:

Assert.AreEqual((int)enumA.one, (int)enumB.one);
查看更多
登录 后发表回答