22

I'd like to know what is default implementation for equality operatort (== and !=)

Is it?

public static bool operator ==(object obj1, object obj2)
{
    return obj1.Equals(obj2);
}
public static bool operator !=(object obj1, object obj2)
{
    return !obj1.Equals(obj2);
}

So I only need to override Equals method or do I need to override euality operators as well ?

2
  • I believe you should search SO, there are a lot of similar topics were discussed so going over few of them you'll got clean understanding of this Commented Sep 8, 2011 at 9:37
  • 4
    Guidelines for Overloading Equals() and Operator == Commented Sep 8, 2011 at 9:38

3 Answers 3

31

No, it's not that - by default, references are checked for equality. Operators such as == are not polymorphic and don't call anything polymorphic by default. So for example:

string x = "Hello";
string y = new String("Hello".ToCharArray());
Console.WriteLine(x == y); // True; uses overloaded operator

object a = x;
object b = y;
Console.WriteLine(a == b); // False; uses default implementation

You can't override equality operators, but you can overload them, as string does. Whether or not you should is a different matter. I think I usually would if I were overriding Equals, but not necessarily always.

Sign up to request clarification or add additional context in comments.

6 Comments

Eric Lippert's evil example: object obj = ""; string str2 = String.Empty; Console.WriteLine(obj == str2); // sometimes true, sometimes false
This is a very good answer and explains many other things along the way. This is where it differs from Equal() method. Equal method is a virtual method so x.Equals(y) would return true since it will be bound during runtime and runtime equivalent of that method is strings Equal method. But == can not be overridden but only overloaded, and which operator will be called is determined during compile-time, and parameter types will be used to determine which overload to get bound to. Thanks @JonSkeet.
While this example is right, the explanation is wrong. The original question has nothing to do with polymorphism, overloading or overriding. The default implementation of == calls ReferenceEquals() not Equals().
@Trade-IdeasPhilip: In what way does the original question have nothing to do with overriding when it asks: "So I only need to override Equals method"? And no, == doesn't call ReferenceEquals - it's the other way round.
He asked is the default implementation ... obj1.Equals(obj2) ...? The answer is no, the default implementation is ... obj1.ReferenceEquals(obj2) ... or something very close to that. That is why overriding Equals() is insufficient.
|
9

The C# language specification, Section 7.9 covers the exact behavior of the built-in == operator. For example, when using == on reference-type values, the following section applies:

7.9.6 Reference type equality operators

The predefined reference type equality operators are:

bool operator ==(object x, object y);
bool operator !=(object x, object y);

The operators return the result of comparing the two references for equality or non-equality.

Since the predefined reference type equality operators accept operands of type object, they apply to all types that do not declare applicable operator == and operator != members. Conversely, any applicable user-defined equality operators effectively hide the predefined reference type equality operators.

[More details skipped...]

Note that "comparing two references for equality" does not mean "the result of calling obj1.Equals(obj2)". It means that both references must point to the same object (reference equality).

3 Comments

It's worth being aware that there are issues with the existing specifications for == and reference equality. It's a remarkably tricky problem to fix - we hope we've fixed it for the next release of the ECMA specification.
@JonSkeet: Sounds interesting. Do you have a link with details?
No, I'm afraid not. All the discussion is private, at least at the moment. (And a lot of it happened in person.) There end up being problems with implicit conversions, IIRC...
3

By default, those operators test for equality of reference.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.