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

Equals and value types

In my book they give a sample Complex class implementation and it
implements:

override bool Equals(object rhs)
{...}

public book Equals(Complex rhs) // IEquatable<Complex>
{...}

public static bool operator==(Complex lhs, Complex rhs)
{...}

My question is, what is the point of implementing Equals when
operator== is defined?

Also, when would the "object" version of Equals ever be called instead
of the specific type version? Obviously it would be called if you
passed an object, but I can't see why that would ever happen in
practice.
Jul 5 '08 #1
1 1153
<cr*****@gmail.comwrote:
In my book they give a sample Complex class implementation and it
implements:

override bool Equals(object rhs)
{...}

public book Equals(Complex rhs) // IEquatable<Complex>
{...}

public static bool operator==(Complex lhs, Complex rhs)
{...}

My question is, what is the point of implementing Equals when
operator== is defined?
Because otherwise hash tables won't work properly - or anything else
which calls Equals.

Also bear in mind that Equals is applied polymorphically whereas ==
isn't.
Also, when would the "object" version of Equals ever be called instead
of the specific type version? Obviously it would be called if you
passed an object, but I can't see why that would ever happen in
practice.
It *often* happens in practice - any time a piece of code compares two
objects without knowing their types at compile-time. Finding an entry
in a list, or looking up a key in a hashtable are the most obvious
examples.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 5 '08 #2

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

Similar topics

1
by: Bijay Kumar | last post by:
Hi Guys, I was going through the source code of Object.cs in rotor. What I found is Equals() implemented as follows: public extern virtual bool Equals(Object obj); What I don't...
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...
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, ...
4
by: Kurt | last post by:
Wouldn't you agree all of the follwoing should produce the same result? r = (o1 == o2); r = (o2 == o1); r = object.Equals(o1, o2); r = object.Equals(o2, o1); r = (o1.Equals(o2)); r =...
4
by: Chris | last post by:
This is something that has been bugging me for some time. When exactly should I be using == instead of .Equals() ? I see a lot of code that performs object evaluation e.g. Is Object1 the same as...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
8
by: Kenneth Baltrinic | last post by:
When one overrides the Equals() method of an object, one is supposed to override GetHashCode() as well and this makes good sense. But I have seen lots of people who do this and do not override the...
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...
28
by: Tony Johansson | last post by:
Hello! I can't figure out what point it is to use GetHashCode. I know that this GetHashCode is used for obtaining a unique integer value. Can somebody give me an example that prove the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.