.NET Forum / Languages / C# / August 2007
Difference, == and Object.Equals
|
|
Thread rating:  |
Jesper, Denmark - 08 Jun 2007 12:33 GMT 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))
Jeroen - 08 Jun 2007 12:48 GMT > 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.
Miroslav Stampar [MCSD.NET / Security+] - 08 Jun 2007 13:05 GMT 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 :)
Jon Skeet [C# MVP] - 08 Jun 2007 13:37 GMT On Jun 8, 1:05 pm, "Miroslav Stampar [MCSD.NET / Security+]" <miroslav.stam...@gmail.com> wrote:
> 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
Göran Andersson - 09 Jun 2007 10:29 GMT > On Jun 8, 1:05 pm, "Miroslav Stampar [MCSD.NET / Security+]" > <miroslav.stam...@gmail.com> wrote: [quoted text clipped - 4 lines] > 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.
> However, object.ReferenceEquals(objA, objB) *will* always check > references, regardless of whether Equals and == are overridden/ > overloaded. > > Jon
 Signature Göran Andersson _____ http://www.guffa.com
Jon Skeet [C# MVP] - 09 Jun 2007 17:04 GMT > > On Jun 8, 1:05 pm, "Miroslav Stampar [MCSD.NET / Security+]" > > <miroslav.stam...@gmail.com> wrote: [quoted text clipped - 7 lines] > 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)); } }
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
Göran Andersson - 09 Jun 2007 20:01 GMT >>> On Jun 8, 1:05 pm, "Miroslav Stampar [MCSD.NET / Security+]" >>> <miroslav.stam...@gmail.com> wrote: [quoted text clipped - 8 lines] > 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.
 Signature Göran Andersson _____ http://www.guffa.com
Jon Skeet [C# MVP] - 09 Jun 2007 20:16 GMT > > 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. 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.
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
Jesper, Denmark - 10 Aug 2007 10:38 GMT Thanks Jon.
> > > But static methods can call overriden methods themselves, which is > > > exactly what the static Equals method does - as I said before. [quoted text clipped - 10 lines] > > In all cases where neither argument is null. Christof Nordiek - 11 Jun 2007 11:55 GMT > 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
Marc Gravell - 11 Jun 2007 12:08 GMT 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
Ben Voigt [C++ MVP] - 12 Jun 2007 08:02 GMT > 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?
Arne Vajhøj - 09 Jun 2007 05:23 GMT > 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
Larry Smith - 09 Jun 2007 15:06 GMT > 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.
Arne Vajhøj - 09 Jun 2007 19:51 GMT >> That is Java. >> [quoted text clipped - 3 lines] > 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 Skeet [C# MVP] - 09 Jun 2007 17:07 GMT > > Sometimes Equals method in custom classes is overriden, so for your > > purpose always use object.Equals(objA, objB). It always checks if objA [quoted text clipped - 4 lines] > 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*.
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
Arne Vajhøj - 09 Jun 2007 19:42 GMT >> 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. Overloaded would be the correct C# terminology.
:-) Arne
Larry Smith - 08 Jun 2007 14:55 GMT http://msdn2.microsoft.com/en-us/library/ms173147(vs.80).aspx
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|