关于通过.Equals()与==运算符进行比较以及对原语与对象进行比较的困惑 [英] Confusion about comparison by .Equals() vs. == operator and primitives vs. objects

查看:82
本文介绍了关于通过.Equals()与==运算符进行比较以及对原语与对象进行比较的困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int a = 0;
short b = 0;
int c = 0;
object a1 = a;
object b1 = b;
object c1 = c;

Console.WriteLine(1);
//comparing primitives - int vs. short
Console.WriteLine(a == b);
Console.WriteLine(b == a);
Console.WriteLine(a.Equals(b));
Console.WriteLine(b.Equals(a));

Console.WriteLine(2);
//comparing objects - int vs. int
Console.WriteLine(c1 == a1);
Console.WriteLine(a1 == c1);
Console.WriteLine(c1.Equals(a1));
Console.WriteLine(a1.Equals(c1));

Console.WriteLine(3);
//comparing objects - int vs. short
Console.WriteLine(a1 == b1);
Console.WriteLine(b1 == a1);
Console.WriteLine(a1.Equals(b1)); //???
Console.WriteLine(b1.Equals(a1));



它将显示以下输出:



It prints this output:

1
True
True
True
False
2
False
False
True
True
3
False
False
False
False



我所知道的;明确



第2章 == 运算符与对象一起使用时返回true仅当它比较内存中由两个不同名称引用的一个对象时(不是很频繁,但可能会发生)。 Equals()方法比较对象的内容(值)。 该站点的许多答案中都提到了它。

What I know; what is clear

Section 2: == operator returns true when used with objects only if it compares one object in memory referenced by two different names (not very frequent, but might happen). Equals() method compare content (value) of the objects. It is mentioned in many answers on this site.

第1部分:使用 = = 运算符,编译器将较小类型转换为较大类型( short 转换为 int )并比较原始值。操作数(变量)的顺序无关紧要。最后一行 Equals()的结果可能会令人困惑,因为它返回false(不比较值),但可以理解。这里的顺序很重要。如 answer 所述,必须选择最佳的重载。通过第一个变量的类型选择它: short.Equals(short)。但是然后 int 不能转换为较小类型( short ),因此没有进行比较并且方法返回

Section 1: Using == operator, compiler converts ‘smaller’ type to ‘bigger’ (short to int) and compares primitive values. Order of operands (variables) doesn’t matter. Result of Equals() in the last line might be confusing, because it returns false (doesn’t compare values), but it is understood. The order matter here. As learned in this answer, the best overload must be selected. It is selected by the type of the first variable: short.Equals(short). But then int cannot be converted to ‘smaller’ type (short), therefore there is no comparison made and method returns false.


  1. 我的理解对吗?

  2. 为什么第3节的最后两行(使用 Equals())都返回false?为什么第1节第3行有区别?为什么不进行过载和值比较?它变得非常抽象,我找不到原因。

  1. Is my understanding above correct?
  2. Why the last two lines of section 3 (using Equals()) both return false? Why is there difference to section 1 line 3? Why isn’t the overload and the value comparison made? It is getting quite abstract and I can’t find the reason.


推荐答案

在第1节中第3行

int a = 0;
short b = 0;
Console.WriteLine(a.Equals(b));

您称 int 的重载为: bool等于(其他int),因为 b (短整数)可以隐式转换为 int ,因此选择了此重载。它返回true。在第3节第3行

you call this overload of int: bool Equals(int other), because b (short) can be implicitly converted to int so this overload is chosen. It returns true. In Section 3 line 3

int a = 0;
short b = 0;
object a1 = a;
object b1 = b;
Console.WriteLine(a1.Equals(b1)); //???

另一个 int 重载(不是 object ,因为 Equals 是虚拟方法)被称为: bool Equals(对象other)。为使其返回true other 应该具有完全相同的类型( int ),但实际上是 short ,因此它返回false。装箱与此处无关,您可以通过以下方式进行验证:

another overload of int (not object, because Equals is virtual method) is called: bool Equals(object other). For it to return true other should have exactly the same type (int), but it's really short so it returns false. Boxing is not relevant here, which you can verify with this:

int a = 0;            
int c = 0;
object a1 = a;
object c1 = c;
// yes, different objects
Console.WriteLine(a1 == c1); // false
// still equal, because both are boxed ints
Console.WriteLine(a1.Equals(c1)); // true

至于理解,我认为文档包含所有相关信息。只需记住:

As for understanding, I think documentation contains all relevant information. Just remember that:


  1. == 和<$ c可以在类中手动定义$ c> Equals 方法,因此理论上可以执行任何操作。您的理解仅与默认行为有关。

  1. Both == operator and Equals method can be manually defined in class and so in theory can do anything. Your understanding relates only to "default" behavior.

== 不是常识,与等于方法不同。因此,当您执行 a1 == b1 - == 时要在编译时定义调用(基于 a1 b1 ),但是当您调用 a1.Equals(b1)-实际上是分派的,因此在运行时定义了调用方法。

== is not virtual in common sense, unlike Equals method. So when you do a1 == b1 - == to be called in defined in compile time (based on types of a1 and b1), but when you call a1.Equals(b1) - it is virtually dispatched so method to call is defined at runtime.

这篇关于关于通过.Equals()与==运算符进行比较以及对原语与对象进行比较的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆