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)) 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.
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 :)
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
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
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
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 <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
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
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
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
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
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
"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
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" <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?
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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, ...
|
by: Fei Li |
last post by:
Hi,
take string class as an example, who can explain the difference?
Thanks
|
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...
|
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
|
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,...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| | |