Search code examples
c#comparisonequals

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


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.

Solution

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