473,396 Members | 1,608 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,396 software developers and data experts.

interactions between operator== and the two Object.Equals methods

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 (reference)
equivalence. You can override it if you like.

Object has ... public virtual bool Equals(object);

The default implementation also gives you a reference equality test.
You can override it if you like.

So ... why two different ways of achieving the same thing? What's one
for? What's the other for? When would you override one? When would you
override the other? I'm asking how these two methods are the same and
also how they are different!!! Let's just consider reference types
only.

I did notice if you implement IComparable you must provide an
implementation of Equals.

I also noticed this advice (Object.Equals help): "if your programming
language supports operator overloading and if you choose to overload
the equality operator for a given type, that type should override the
Equals method. Such implementations of the Equals method should return
the same results as the equality operator."

If they need to be consistent why *two* different ways to achieve the
same functionality - isn't this asking for trouble?!

And then it says this seemingly contradictory quote: "most reference
types should not overload the equality operator, even if they override
Equals."

Eek - it's pretty clear now that I need some help from someone
understanding the rationale behind == and .Equals!!!

As a finale, what's the point of the static Equals method on Object?
public static bool Equals(object objA, object objB);

What does it do that objA.Equals(objB) would not achieve?

Cheers for listening and hope someone can help my lack of
understanding!

Emma Middlebrook
em**************@fastmail.fm
Nov 15 '05 #1
2 9344
Hello!

Equals is also used when comparing e.g. strings. You can implement your own
ways to compare objects. A wellknown fact, but I just wanted to point it out
to you! :)

--
venlig hilsen / with regards
anders borum
--
Nov 15 '05 #2

"emma middlebrook" <em**************@fastmail.fm> wrote in message
news:e2**************************@posting.google.c om...
Hi
Having difficulty getting myself clear on how a type's operator== fits in with Object.Equals.


I suspect some of the confusion comes from the fact that not all .Net
languages support operator overloading (e.g. vb.net). Those langauges
will still allow you to override object.Equals. In C# we get the
additional benefit of having more concise code.

That is, if we implemented ComplexNumber as a class (even though a
struct probably makes more sense):
ComplexNumber a = new ComplexNumber (10, 20);
ComplexNumber b = new ComplexNumber (10, 20);

we could override equals to provide syntax like
a.Equals(b)
in order to get value-type equality checking.
We should also override the static method so that
a.Equals(b) is the same as ComplexNumber.Equals(a,b);

In C#, we would probably want to override the operator also, thus
(a==b) would return true.

I believe the online help was saying that if you've changed the ==
operator to make it more convenient to compare using value-type
semantics, then you should change the Equals method so that they both
"do the same thing". That is, it would be disturbing in the above
example if
a==b returned true but a.Equals(b) returned false. That would only
happen if you overload the operator but forget the method.

For most reference types, overloading the operator == is a bad idea,
IMO. Other languages will assume that the == operator means reference
type equality. They would only be used to Equals() to test for value
equality.

In the above example, it would be fine to have an implementation
whereby
a.Equals(b) = true (since they have similar values)
but have a == b still performing reference equality and hence return
false (since the two objects are unique instances of ComplexNumber).

I guess the help should have said that (a == b) should always behave
the same as either a.Equals(b) or Object.ReferenceEquals(a,b), with
the preference being ReferenceEquals.

HTH,
Mike
Nov 15 '05 #3

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

Similar topics

45
by: Jordan Rastrick | last post by:
Can anybody please give me a decent justification for this: class A(object): def __init__(self, a): self.a = a def __eq__(self, other): return self.a == other.a s = A(3)
3
by: #Hai | last post by:
Hi, What is the difference between Object.Equals and "==" operator ? When we use CollectionBase.List.Remove(object), which methods is used to compare objects ? Thanks
4
by: Peter | last post by:
The Operator overloading in C# has one limitation which I have not seen stated explicitly anywhere. Take the following example public class A { int m_A; public A(int a) {
17
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: ...
5
by: Brian Keating | last post by:
Hi there, Just wondering what is the correct way of implementing the comparison operator in c# operator== My problem is when one or more of the objects is null, I've cast to object to try...
8
by: Pesso | last post by:
I'm having a difficulty compairing null to a class object whose "equal" operator is overridden.. Consider the following: class Foo { // ... public static bool operator==(Foo f1, Foo f2) {...
9
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
12
by: cody | last post by:
Why can I overload operator== and operator!= separately having different implementations and additionally I can override equals() also having a different implementation. Why not forbid...
12
by: mathboy2 | last post by:
Can anyone tell me what I'm doing wrong in the below snippet of code? I'm simply trying to override the == operator but having no luck. I can't even get it to hit a break point at the top of the ==...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.