简体   繁体   English

c#enum equals()vs ==

[英]c# enum equals() vs ==

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? 他们使用一个与另一个的重要考虑因素是什么?

If the compile-time type of enumInstance is the enum type, you're fine with == . 如果编译时类型的enumInstance是枚举类型,那么你可以使用==

If the compile-time type of enumInstance is Enum , ValueType or Object , you need to use Equals . 如果enumInstance的编译时类型是EnumValueTypeObject ,则需要使用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 . 请注意,您的枚举当前违反了.NET命名约定 - 通常是MyEnum.Value

Using == instead of Equals is a bit quicker, there is no need to box enums and no functions calls needed here is sample c# code and generated MSIL for it: 使用==而不是Equals更快一点,不需要框枚举,这里不需要函数调用样本c#代码并为它生成MSIL:

 class Program
    {
        static void Main(string[] args)
        {
            var instance = MyEnum.First;

            if (instance == MyEnum.First)
            {
                Console.WriteLine("== Called");
            }

            if (instance.Equals(MyEnum.First))
            {
                Console.WriteLine("Equals called");
            }

        }     
    }

    enum MyEnum { First = 99, Second = 100}

MSIL: MSIL:

IL_0000:  nop
  IL_0001:  ldc.i4.s   99
  IL_0003:  stloc.0
  IL_0004:  ldloc.0
  IL_0005:  ldc.i4.s   99
  IL_0007:  ceq
  IL_0009:  ldc.i4.0
  IL_000a:  ceq
  IL_000c:  stloc.1
  IL_000d:  ldloc.1
  IL_000e:  brtrue.s   IL_001d
  IL_0010:  nop
  IL_0011:  ldstr      "== Called"
  IL_0016:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_001b:  nop
  IL_001c:  nop
  IL_001d:  ldloc.0
  IL_001e:  box        ConsoleApplication1.MyEnum
  IL_0023:  ldc.i4.s   99
  IL_0025:  box        ConsoleApplication1.MyEnum
  IL_002a:  callvirt   instance bool [mscorlib]System.Object::Equals(object)
  IL_002f:  ldc.i4.0
  IL_0030:  ceq
  IL_0032:  stloc.1
  IL_0033:  ldloc.1
  IL_0034:  brtrue.s   IL_0043
  IL_0036:  nop
  IL_0037:  ldstr      "Equals called"
  IL_003c:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_0041:  nop
  IL_0042:  nop
  IL_0043:  ret

As you can see == generates ceq instruction, Equals method performes boxing and callvirt 如你所见==生成ceq指令,Equals方法执行拳击和callvirt

There is a case that the other answers here have not mentioned that may help others. 有一种情况,这里的其他答案没有提到可以帮助他人。

With c# the underlying type of an enum is an integral. 使用c#,枚举的基础类型是不可或缺的。 Because it is an integral, you can logically OR the enums together. 因为它是不可或缺的,所以您可以将枚举逻辑OR组合在一起。

When using either of the above methods for equality will fail if enums are logically OR'd together. 如果枚举在逻辑上与OR一起使用,则使用上述任一方法进行相等操作将失败。

So for some special cases, such as using enums as flags, you will need to logically AND the case you are testing for with first prior to checking for equality. 因此,对于某些特殊情况,例如使用枚举作为标志,在检查相等性之前,您需要首先逻辑地对要测试的情况进行AND。

if ((enumInstance & MyEnum.SomeValue).Equals(MyEnum.SomeValue))

or 要么

if ((enumInstance & MyEnum.SomeValue) == MyEnum.SomeValue)

Strictly speaking it's safest to use "==" with enums. 严格来说,使用“==”与枚举最安全。

A complete list of possible enum types can be found here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/enum 可以在此处找到可能的枚举类型的完整列表: https//docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/enum

As an extension to Jon Skeet's old answer, it is true that you get a compilation error when you compare Enum == YourActualEnum.Value , however when you do Enum == Enum , which compiles fine, it return false all the time. 作为Jon Skeet旧答案的扩展,当您比较Enum == YourActualEnum.Value时,确实会出现编译错误,但是当您执行Enum == Enum ,编译正常,它会一直返回false。

public class TestClass
{
    public bool TestMethod1()
    {
        bool Result = false;

        Enum l_Value = TEST_ENUM.TEST_VALUE_1;

        Enum l_Check_Value = TEST_ENUM.TEST_VALUE_1;

        Result = l_Value == l_Check_Value;

        return Result;
    }

    public bool TestMethod2()
    {
        bool Result = false;

        TEST_ENUM l_Value = TEST_ENUM.TEST_VALUE_1;

        TEST_ENUM l_Check_Value = TEST_ENUM.TEST_VALUE_1;

        Result = l_Value == l_Check_Value;

        return Result;
    }

    public bool TestMethod3()
    {
        bool Result = false;

        Enum l_Value = TEST_ENUM.TEST_VALUE_1;

        Enum l_Check_Value = TEST_ENUM.TEST_VALUE_1;

        Result = l_Value.Equals(l_Check_Value);

        return Result;
    }

    public enum TEST_ENUM
    {
        TEST_VALUE_1,
        TEST_VALUE_2,
        TEST_VALUE_3
    }
}

if you try the following on a test app you'll get the following 如果您在测试应用上尝试以下操作,您将获得以下内容

Console.WriteLine("Method 1 result: {0}", myClass.TestMethod1());

Console.WriteLine("Method 2 result: {0}", myClass.TestMethod2());

Console.WriteLine("Method 3 result: {0}", myClass.TestMethod3());

you will get the following results 你会得到以下结果

Method 1 result: False Method 2 result: True Method 3 result: True

If you wonder why would you ever compare Enum against Enum....I discovered it while trying to be smart while creating an EnumConverter and a FlagConvert for a WPF project. 如果你想知道为什么你会将Enum与Enum进行比较....我在为WPF项目创建EnumConverter和FlagConvert时尝试变聪明时发现了它。 There you receive only an object value as parameter and for the flag converter specifically i wanted to provide a special text for when no flag was selected (ie the enum has a value of 0 which has no static member for it). 在那里你只收到一个对象值作为参数,对于标志转换器,我特别希望在没有选择标志时提供一个特殊的文本(即枚举的值为0,没有静态成员)。

Nothing else worked (including value.Equals(0), value.Equals((int)0) ) except this: 没有其他工作(包括value.Equals(0),value.Equals((int)0))除了这个:

l_Source_Type = value.GetType();

if (l_Source_Type.IsDefined(typeof(FlagsAttribute)))
{
    Enum l_Value = (Enum)value;

    Enum l_Check_Value = (Enum)Enum.ToObject(l_Source_Type, 0);

    if (l_Value.Equals(l_Check_Value))
    {
        return String.Empty;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM