Pages

Thursday, February 27, 2014

C#: Inconsistent behavior of equality

Learning AngularJS; Guide for beginners:

In C# equality is quite confusing, you might see many cases where obvious thing result in an unexpected result.










Here are some cases to illustrate a bit of different output of equalities.

see here demo

int _Int = 1;
short _Short = 1;
object _objectInt1 = _Int;
object _objectInt2 = _Int;
object _objectShort = _Short;
Console.WriteLine(_Int == _Short);   // case 1 true
Console.WriteLine(_Short == _Int);   // case 2 true
Console.WriteLine(_Int.Equals(_Short));   // case 3 true
Console.WriteLine(_Short.Equals(_Int));   // case 4 false
Console.WriteLine(_objectInt1 == _objectInt1);   // case 5 true
Console.WriteLine(_objectInt1 == _objectShort);  // case 6 false
Console.WriteLine(_objectInt1 == _objectInt2);   // case 7 false
Console.WriteLine(Equals(_objectInt1, _objectInt2));  // case 8 true
Console.WriteLine(Equals(_objectInt1, objectShort)); // case 9 false

How these cases are working?


Console.WriteLine(_Int == _Short); // case 1 true
Console.WriteLine(_Short == _Int); // case 2 true


For case one and two we must first determine what the == operator means here. C# defines over a dozen different built-in == operators:
string == string
object == object
ulong == ulong
uint == uint
int == int
long == long
...
There is no comparison operator for different data types comparison i.e, int == short or short == int, So for this there other possibilities in available operator will be determined, the best match for the case is int == int or short == short but implicit conversion of int to short is not possible and error prone. So the short is converted to int and then the two values are compared as numbers. They are therefore equal as both holds the same value.

Console.WriteLine(_Int.Equals(_Short)); // case 3 true


In case three we must first find which overloaded methods from available methods is invoked here. The Equals() here is called by a type int and it has three methods named Equals:
Equals(object, object) // static method from object
Equals(object)         // virtual method from object
Equals(int)            // Implements IEquatable.Equals(int)
The first one we can eliminate because there are different count of arguments, Of the other two, the unique best method is the one that takes an int; it is always better to convert the short argument to int than to object. Therefore we call Equals(int), which then compares the two integers again using value equality, so this is true.

Console.WriteLine(_Short.Equals(_Int)); // case 4 false!


In scenario four we again must determine what Equals means. The receiver is of type short which again has three methods named Equals
Equals(object, object) // static method from object
Equals(object)         // virtual method from object
Equals(short)          // Implements IEquatable.Equals(short)
Overload resolution eliminates the first because there are too few arguments and eliminates the third because there is no implicit conversion from int to short. That leaves short.Equals(object), which has the moral equivalent of this implementation:
bool Equals(object z)
{
  return z is short && (short)z == this;
}
That is, for this method to return true the argument passed in must be a boxed short, and when unboxed it must be equal to the receiver. Since the argument is a boxed int, this returns false. There is no special gear in this implementation that says “well, what if I were to convert myself to the type of the argument and then compare?”

Console.WriteLine(_objectInt1 == _objectInt1); // case 5 true
Console.WriteLine(_objectInt1 == _objectShort); // case 6 false!!
Console.WriteLine(_objectInt1 == _objectInt2); // case 7 false!!!



In scenarios five, six and seven operator overload resolution chooses the object == object form, which is equivalent to a call to Object.ReferenceEquals. Clearly the two references are equal in case five and unequal in cases six and seven. Whether the values of the objects when interpreted as numbers are equal does not come into play at all; only reference equality is relevant.


Console.WriteLine(Equals(_objectInt1, _objectInt2)); // case 8 true
Console.WriteLine(Equals(_objectInt1, objectShort)); // case 9 false

In scenarios eight and nine operator overload resolution chooses the static method Object.Equals, which you can think of as being implemented like this:
public static bool Equals(object x, object y)
{
    if (ReferenceEquals(x, y)) return true;
    if (ReferenceEquals(x, null)) return false;
    if (ReferenceEquals(y, null)) return false;
    return x.Equals(y);
}
In scenario eight we have two references that are unequal and not null; therefore we call int.Equals(object), which as you would expect from our previous discussion of short.Equals(object) is implemented as the moral equivalent of:
bool Equals(object z)
{
  return z is int && (int)z == this;
}
Since the argument is of type int it is unboxed and compared by value. In scenario nine the argument is a boxed short and so the type check fails and this is false.


We discussed nine different methods that two things can be compared for equality, despite the fact that we have always same input of 1 in both side of comparison, equality is true in only half the cases. If you think this is crazy and confusing, you’re right! Equality is tricky in C#.

2 comments:

  1. c# default for comparing objects is by reference. If you want to compare something else, expecially different datatypes, you have to check the documentation. Boxing basic types is mostly a bad idea.

    In many cases the compiler will throw a warning, if you do a unexpected reference compare.

    Netherless it's nice to see the problem pointed out.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete