473,396 Members | 2,013 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.

Revisit Object.Equals

Hi All,

Just been re-reading Applied MS .Net Framework Programming (been about
2 years since I last read it) and came across the chapter on Equals,
==, and GetHashCode. It dawned on me that after 2 years of c# .net
programming I have never overridden any of these methods.

But why would I? For example, I have a simple class:

class MC
{
int i;

MC (int i)
{
this.i = i;
}
}

If I then implemented:

MC c1, c2;
c1 = new MC (2);
c2 = new MC (2);

bool theSame = (c1 == c2); // Would this return false because
different instances?
bool theSame2 = (c1.Equals (c2)); // Is this the same as the above?

Apr 9 '06 #1
4 1345
Well, what happened when you tried it?
"nick_nw" <ng****@gmail.com> wrote in message
news:11*********************@t31g2000cwb.googlegro ups.com...
Hi All,

Just been re-reading Applied MS .Net Framework Programming (been about
2 years since I last read it) and came across the chapter on Equals,
==, and GetHashCode. It dawned on me that after 2 years of c# .net
programming I have never overridden any of these methods.

But why would I? For example, I have a simple class:

class MC
{
int i;

MC (int i)
{
this.i = i;
}
}

If I then implemented:

MC c1, c2;
c1 = new MC (2);
c2 = new MC (2);

bool theSame = (c1 == c2); // Would this return false because
different instances?
bool theSame2 = (c1.Equals (c2)); // Is this the same as the above?

Apr 9 '06 #2
Hello nick_nw,

It depends on what you are trying to compare - equality or reference identity.
Jon gave good explanation http://www.yoda.arachsys.com/csharp/faq/#equals

For value types you need to override Equals to gain performance, because
standard realization uses reflection for this

n> Hi All,
n>
n> Just been re-reading Applied MS .Net Framework Programming (been
n> about 2 years since I last read it) and came across the chapter on
n> Equals, ==, and GetHashCode. It dawned on me that after 2 years of
n> c# .net programming I have never overridden any of these methods.
n>
n> But why would I? For example, I have a simple class:
n>
n> class MC
n> {
n> int i;
n> MC (int i)
n> {
n> this.i = i;
n> }
n> }
n> If I then implemented:
n>
n> MC c1, c2;
n> c1 = new MC (2);
n> c2 = new MC (2);
n> bool theSame = (c1 == c2); // Would this return false because
n> different instances?
n> bool theSame2 = (c1.Equals (c2)); // Is this the same as the
n> above?
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Apr 9 '06 #3
Why would you override equals/==?
For example, if you wanted to use instances of particular classes as keys in
a hashtable. Or if you wanted to have a list of instances and wanted to know
if you have equal instances inside.
Basically, if you wanted to provide equality semantics for reference types
based on field values instead of object identities.

"nick_nw" <ng****@gmail.com> wrote in message
news:11*********************@t31g2000cwb.googlegro ups.com...
Hi All,

Just been re-reading Applied MS .Net Framework Programming (been about
2 years since I last read it) and came across the chapter on Equals,
==, and GetHashCode. It dawned on me that after 2 years of c# .net
programming I have never overridden any of these methods.

But why would I? For example, I have a simple class:

class MC
{
int i;

MC (int i)
{
this.i = i;
}
}

If I then implemented:

MC c1, c2;
c1 = new MC (2);
c2 = new MC (2);

bool theSame = (c1 == c2); // Would this return false because
different instances?
bool theSame2 = (c1.Equals (c2)); // Is this the same as the above?

Apr 9 '06 #4
I'm at home, and I don't have C#/VS here. I'l; try it on Monday at
work, hence ng question.

Apr 9 '06 #5

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

Similar topics

3
by: Asad Khan | last post by:
public boolean equals(Object o) { if (!(o.equals(null))) { // * if (o instanceof foo) { if (this.equals(o)) { return true; } } } return false; }
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...
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...
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: Arjen | last post by:
Hi, I have a class with some attributes. For example class person with name as attribute. I have add multiple persons in an arraylist. Now I need a function to get/find a person by the name...
16
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
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...
2
by: mjrtom19 | last post by:
I have a Jframe using a menubar to determine the color and shape of a graphics2d object, at the moment the color stays red no matter what. I think the problem is in the itemListener in the inner...
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?
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.