473,395 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Difference, == and Object.Equals

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))

Jun 8 '07 #1
17 1955
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.

Jun 8 '07 #2
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 :)

Jun 8 '07 #3
On Jun 8, 1:05 pm, "Miroslav Stampar [MCSD.NET / Security+]"
<miroslav.stam...@gmail.comwrote:
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

Jun 8 '07 #4
Miroslav Stampar [MCSD.NET / Security+] 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.
That is Java.

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

Arne
Jun 9 '07 #6
Jon Skeet [C# MVP] wrote:
On Jun 8, 1:05 pm, "Miroslav Stampar [MCSD.NET / Security+]"
<miroslav.stam...@gmail.comwrote:
>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.
However, object.ReferenceEquals(objA, objB) *will* always check
references, regardless of whether Equals and == are overridden/
overloaded.

Jon

--
Göran Andersson
_____
http://www.guffa.com
Jun 9 '07 #7
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.
Jun 9 '07 #8
Göran Andersson <gu***@guffa.comwrote:
Jon Skeet [C# MVP] wrote:
On Jun 8, 1:05 pm, "Miroslav Stampar [MCSD.NET / Security+]"
<miroslav.stam...@gmail.comwrote:
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.

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));
}
}

--
Jon Skeet - <sk***@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
Jun 9 '07 #9
Arne Vajhřj <ar**@vajhoej.dkwrote:
Miroslav Stampar [MCSD.NET / Security+] 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.
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 Skeet - <sk***@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
Jun 9 '07 #10
Jon Skeet [C# MVP] wrote:
Arne Vajhřj <ar**@vajhoej.dkwrote:
>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
Jun 9 '07 #11
Larry Smith wrote:
>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.
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
Jun 9 '07 #12
Jon Skeet [C# MVP] wrote:
Göran Andersson <gu***@guffa.comwrote:
>Jon Skeet [C# MVP] wrote:
>>On Jun 8, 1:05 pm, "Miroslav Stampar [MCSD.NET / Security+]"
<miroslav.stam...@gmail.comwrote:
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
_____
http://www.guffa.com
Jun 9 '07 #13
Göran Andersson <gu***@guffa.comwrote:
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.

--
Jon Skeet - <sk***@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
Jun 9 '07 #14
"Jon Skeet [C# MVP]" <sk***@pobox.comschrieb
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
Jun 11 '07 #15
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
Jun 11 '07 #16

"Marc Gravell" <ma**********@gmail.comwrote in message
news:um**************@TK2MSFTNGP05.phx.gbl...
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?

Jun 12 '07 #17
Thanks Jon.

"Jon Skeet [C# MVP]" wrote:
Göran Andersson <gu***@guffa.comwrote:
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.

--
Jon Skeet - <sk***@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
Aug 10 '07 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bijay Kumar | last post by:
Hi Guys, I was going through the source code of Object class (Object.cs in rotor). What I found is Equals() implemented as follows: public extern virtual bool Equals(Object obj); What...
2
by: emma middlebrook | last post by:
Hi Having difficulty getting myself clear on how a type's operator== fits in with Object.Equals. Let's just consider reference types. The default operator== tests for object identity...
2
by: Kevin B Ebert | last post by:
Today, I ran into something I didn't quite expect. I'm sure there is a logical explanation for it so I want to post it and see if anyone can explain the difference to me. I came across a...
17
by: Zeng | last post by:
I'm trying to comparing 2 objects (pointer to object) to see if they are the "same" as each other. Here is what the definition of being the "same" object type for both objects, object 1, ...
3
by: Fei Li | last post by:
Hi, take string class as an example, who can explain the difference? Thanks
5
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but...
3
by: Andreas Klemt | last post by:
Hello, what is the difference and what is better (performance etc) ? a) If myDate.Equals(Date.MinValue) b) If myDate = Date.MinValue Thanks, Andreas
12
by: Nathan Sokalski | last post by:
What is the difference between the Page_Init and Page_Load events? When I was debugging my code, they both seemed to get triggered on every postback. I am assuming that there is some difference,...
5
by: taumuon | last post by:
I've got an object, Person, that supports IEquatable<Person>. It implements bool Equals(Person obj) as well as overriding bool Equals(object obj) I've got a container type that holds a member...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.