Difference, == and Object.Equals

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

What is the difference of == and Object.Equals()

If I want to query werther to pointers are pointing to same instance, do I
use == or Objects.Equals?

foo a,b;

if ( a == b ) ... or

if (a.Equals(b))
 
foo a,b;
if ( a == b ) ... or

if (a.Equals(b))

In this example, the first statement will check wether 'a' and 'b'
refer to the same foo instance.

The second statement will call the 'Equals' method of foo if
overridden, or the object 'Equals' method. Typically you may check if
several/all fields of a and b have the same value. If the foo class
overrides the equals method, it should (according to msdn) guarantee
some things:

- x.Equals(x) returns true.
- x.Equals(y) returns the same value as y.Equals(x).
- if (x.Equals(y) && y.Equals(z)) returns true, then x.Equals(z)
returns true.
- Successive invocations of x.Equals(y) return the same value as long
as the objects referenced by x and y are not modified.
- x.Equals(null) returns false.
 
Sometimes Equals method in custom classes is overriden, so for your
purpose always use object.Equals(objA, objB). It always checks if objA
and objB refer to the same object.

HTH :)
 
Sometimes Equals method in custom classes is overriden, so for your
purpose always use object.Equals(objA, objB). It always checks if objA
and objB refer to the same object.

No it doesn't. It will call the overridden Equals method if both objA
and objB are non-null.

However, object.ReferenceEquals(objA, objB) *will* always check
references, regardless of whether Equals and == are overridden/
overloaded.

Jon
 
Miroslav said:
Sometimes Equals method in custom classes is overriden, so for your
purpose always use object.Equals(objA, objB). It always checks if objA
and objB refer to the same object.

That is Java.

In C# == can be overridden too and I would consider it best
practice to override both Equals and ==.

Arne
 
Jon said:
No it doesn't. It will call the overridden Equals method if both objA
and objB are non-null.

Yes, it does. The Object.Equals(object, object) method is a static
method in the Object class. Static methods can not be overridden.
 
That is Java.
In C# == can be overridden too and I would consider it best
practice to override both Equals and ==.

Not according to MSFT who specifically advises against overriding == for
non-immutable types.
 
Göran Andersson said:
Yes, it does. The Object.Equals(object, object) method is a static
method in the Object class. Static methods can not be overridden.

But static methods can call overriden methods themselves, which is
exactly what the static Equals method does - as I said before.

Put it this way: if object.Equals(object, object) doesn't call the
overridden Equals method, and only determines reference equality,
please explain why the following program prints False then True:

using System;
using System.Text;

class Test
{
static void Main()
{
string a = new StringBuilder("Hello").ToString();
string b = new StringBuilder("Hello").ToString();

Console.WriteLine (object.ReferenceEquals(a,b));
Console.WriteLine (object.Equals(a,b));
}
}
 
Arne Vajhøj said:
That is Java.

In C# == can be overridden too and I would consider it best
practice to override both Equals and ==.

Just to be picky, == can't be overridden - it can be overloaded. The
difference is important. For instance, if we do:

using System;
using System.Text;

class Test
{
static void Main()
{
object a = new StringBuilder("Hello").ToString();
object b = new StringBuilder("Hello").ToString();

Console.WriteLine(a==b);
}
}

that will print False, because ==(object,object) is used instead of
the ==(string,string) *overloaded* which is provided by the string
class - whereas a.Equals(b) will print True because the Equals method
is *overridden*.
 
Jon said:
Just to be picky, == can't be overridden - it can be overloaded. The
difference is important.

Overloaded would be the correct C# terminology.

:-)

Arne
 
Larry said:
Not according to MSFT who specifically advises against overriding == for
non-immutable types.

True.

But f.ex. FxCop docs state:

"The equality operator is intended to be a syntactically convenient way
of accessing the functionality of the Equals method."

Arne
 
Jon said:
Göran Andersson said:
Jon said:
On Jun 8, 1:05 pm, "Miroslav Stampar [MCSD.NET / Security+]"
Sometimes Equals method in custom classes is overriden, so for your
purpose always use object.Equals(objA, objB). It always checks if objA
and objB refer to the same object.
No it doesn't. It will call the overridden Equals method if both objA
and objB are non-null.
Yes, it does. The Object.Equals(object, object) method is a static
method in the Object class. Static methods can not be overridden.

But static methods can call overriden methods themselves, which is
exactly what the static Equals method does - as I said before.

I see. So by "it", you didn't mean the call, but the method. The call
goes to the static method, but if you say that the method in turn calls
a virtual method in certain cases, I take your word for it.
 
Göran Andersson said:
I see. So by "it", you didn't mean the call, but the method.

Exactly - which is what I understood Miroslav to mean as well; if "it"
is the compiler in his post, then no method is involved in the first
place.
The call goes to the static method, but if you say that the method in
turn calls a virtual method in certain cases, I take your word for
it.

In all cases where neither argument is null.
 
Jon Skeet said:
No it doesn't. It will call the overridden Equals method if both objA
and objB are non-null.

from the Docs of Object.Equals(Object, Object):

true if objA is the same instance as objB or if both are null references or
if objA.Equals(objB) returns true; otherwise, false

From this objA.Equals(objB) will be called also if objB is null.
Also, if both are the same object, Equals wouldn't be called. But this only
makes a difference if Equals is implented badly in the type of objA.

Christof
 
The documentation may be a little misleading... the implementation
(courtesy of reflector) only calls objA.Equals(objB) if both are
non-null. The documentation, for instance, doesn't even assert that
objA is non-null, so this would error if implemented as per the docs.

Marc
 
Marc Gravell said:
The documentation may be a little misleading... the implementation
(courtesy of reflector) only calls objA.Equals(objB) if both are non-null.
The documentation, for instance, doesn't even assert that objA is
non-null, so this would error if implemented as per the docs.

YADE (Yet Another Documentation Error)

Is this just poor work, or is Microsoft actively leaving out core details in
order to detect people reverse engineering?
 
Thanks Jon.

Jon Skeet said:
Exactly - which is what I understood Miroslav to mean as well; if "it"
is the compiler in his post, then no method is involved in the first
place.


In all cases where neither argument is null.
 
Back
Top