Confusion about comparison by .Equals() vs. == operator and primitives vs. objects

≯℡__Kan透↙ 提交于 2020-01-04 04:56:43

问题


Consider this code:

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

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.

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.

Questions:

  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:


In section 1 line 3

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

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)); //???

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. 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.

  2. == 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.




回答2:


In addition to boxing which means that a value type will result in a different reference in memory you have to consider Implicit Numeric Conversions in fact, for this reason, you have

Console.WriteLine(a.Equals(b)); 

which gives you a true but not this

Console.WriteLine(b.Equals(a));

here another example

static void Main(string[] args)
        {


            int i = 0;
            long L = 0;


            Console.WriteLine(1);
            //comparing primitives - int vs. short

            Console.WriteLine(L.Equals(i)); //true
            Console.WriteLine(i.Equals(L));//false
        }


来源:https://stackoverflow.com/questions/46829332/confusion-about-comparison-by-equals-vs-operator-and-primitives-vs-objec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!