c# enum equals() vs ==

ToddBFisher picture ToddBFisher · Jul 5, 2013 · Viewed 36.2k times · Source

In the case of using enums, is it better to use:

if (enumInstance.Equals(MyEnum.SomeValue))

or to use

if (enumInstance == MyEnum.SomeValue)

Are their any important considerations using one vs the other?

Answer

Jon Skeet picture Jon Skeet · Jul 5, 2013

If the compile-time type of enumInstance is the enum type, you're fine with ==.

If the compile-time type of enumInstance is Enum, ValueType or Object, you need to use Equals. (You'll get a compile-time error if you try to use == in that case.)

Note that your enum currently violates .NET naming conventions - it would normally be MyEnum.Value.