简体   繁体   English

使用Equals比较Enum和const int时未出现编译错误

[英]Did not get compile error when comparing Enum with const int using Equals

C#: Code parts: C#:代码部分:

class ConstValues
{
   public const int NULL=1;
}

class Example
{
   private enum FormatFunction
   {
      Date,
      Unknown
   }

   ...

   FormatFunction returnValue = fn();

   ...

Now I have two scenarios. 现在我有两种情况。

When I used this way to compare my return code to a value 当我使用这种方法将返回码与值进行比较时

if (!returnValue.Equals(ConstValues.NULL))
{
   ...

I get no compile time error (and the code does not work as intended because this is a bug of mine that I missed out). 我没有编译时错误(并且代码无法按预期工作,因为这是我遗漏的我的错误)。

But when I change to 但是当我改变为

if (returnValue != ConstValues.NULL)
{
   ...

I get a compile time error and discover the mistake I did. 我收到一个编译时错误,发现了我做的错误。

I understand that the underlying structure of an enum is int, but I would rather get a compile time error even when using Equals. 我知道枚举的底层结构是int,但是即使使用Equals,我还是会遇到编译时错误。

Why do the first way pass and the second don't? 为什么第一种方法通过而第二种方法没有通过?

It is because default virtual Equals method is receiving object instead of strong typed value like in second example. 这是因为默认的虚拟Equals方法正在接收对象,而不是第二个示例中的强类型值。 It is boxed into object and will check types only in runtime. 它装在对象中,仅在运行时检查类型。

Why it is made so? 为什么这样做呢? The reasoning is pretty funny - for possibility to compare cats and dogs if they walk on all fours. 推理非常有趣-如果猫和狗四肢走路,可以比较它们。 Compare two completely different objects by some almost identical properties. 通过一些几乎相同的属性比较两个完全不同的对象。

Thing is, when you can - work with strong typed objects. 问题是,当您可以时-使用强类型对象。 This will prevent bad things from happening in compile time. 这样可以防止在编译时发生不良情况。

!= is a language convention so this is C# specific. !=是一种语言约定,因此这是C#特定的。 Calling this operator is early bound , in other words, it will occur at compile time. 调用此运算符是早绑定的 ,换句话说,它将在编译时发生。

Equals is a framework convention, .NET in this case, and that is bound at runtime. Equals是一种框架约定,在这种情况下为.NET,它在运行时绑定。

When you call != , the decision is made by the C# compiler during compilation so you get an error. 当您调用!= ,由C#编译器在编译过程中做出决定,因此会出现错误。 When you call Equals the decision is made by the framework at runtime. 当您调用Equals时,框架将在运行时做出决定。 Since your enum is not of type object, it will be turned into an object (boxing) and then the runtime will check if your type has overridden the Equals method, since you have not, it will use the default implementation. 由于您的枚举不是对象类型,它将被转换为对象(装箱),然后运行时将检查您的类型是否重写了Equals方法,因为您没有,则将使用默认实现。


Equals for Reference type Equals参考类型

If the instance is a reference type, the default implementation of Equals checks if one object reference is the same as the other object reference. 如果实例是引用类型,则Equals的默认实现将检查一个对象引用是否与另一个对象引用相同。 If they are the same reference, it returns true. 如果它们是相同的引用,则返回true。 Otherwise it returns false. 否则返回false。


Equals for Value type Equals值类型

If the instance is a value type, then it will test for value equality. 如果实例是值类型,则它将测试值是否相等。 This is your case. 这是你的情况。 It will check if the enum value you have is equal to the constant value. 它将检查您的枚举值是否等于常数。 No error will be shown or thrown: it either equals or it does not. 不会显示或抛出任何错误:相等或不相等。

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

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