473,289 Members | 2,087 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,289 software developers and data experts.

Overriding Equals()

Why is it that when I override Equals I get the following warning? What am I
suppose to do?

warning CS0659: 'XXX.MyObject' overrides Object.Equals(object o) but does
not override Object.GetHashCode()
Nov 15 '05 #1
4 6237
David Hoffer <dh************@xrite.remove.com> wrote:
Why is it that when I override Equals I get the following warning? What am I
suppose to do?

warning CS0659: 'XXX.MyObject' overrides Object.Equals(object o) but does
not override Object.GetHashCode()


If you override Equals but not GetHashCode, your objects won't behave
well if used as keys in a Hashtable. See the docs for Object.Equals for
more information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
David,

Think of a hashcode as a quick way of uniquely identifying similar
instances of a type. It is recommended that when you override Equals (which
is used for comparisons) that you override GetHashCode to produce the same
result for two instances that will return true. The reason for this is that
the hashcode is used for comparisons as well (for things like Hashtables).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"David Hoffer" <dh************@xrite.remove.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Why is it that when I override Equals I get the following warning? What am I suppose to do?

warning CS0659: 'XXX.MyObject' overrides Object.Equals(object o) but does
not override Object.GetHashCode()

Nov 15 '05 #3
I didn't see anything in the docs on this....

-dh

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
David Hoffer <dh************@xrite.remove.com> wrote:
Why is it that when I override Equals I get the following warning? What am I suppose to do?

warning CS0659: 'XXX.MyObject' overrides Object.Equals(object o) but does not override Object.GetHashCode()


If you override Equals but not GetHashCode, your objects won't behave
well if used as keys in a Hashtable. See the docs for Object.Equals for
more information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #4
David Hoffer <dh************@xrite.remove.com> wrote:
I didn't see anything in the docs on this....


Open the docs for Object.Equals, then click on the instance method
rather than the static method. Under the list of bullet points, it has:
"See GetHashCode for additional required behaviors pertaining to the
Equals method." It also has (a few lines later): "Types that override
Equals must also override GetHashCode; otherwise, Hashtable might not
work correctly."

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

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

Similar topics

4
by: Rafael Veronezi | last post by:
I have some questions about override in inheritance, and virtual members. I know that you can you override a method by two ways in C#, one, is overriding with the new keyword, like: public new...
3
by: Frank Wisniewski | last post by:
Is this suppose to work like this: I have a class called foo in which I tried to override equals and return different answers based on what was passed in. public class foo { private string...
1
by: Anders Borum | last post by:
Hello! I have a framework where all objects are uniquely identified by a GUID (Global Unique Identifier). The objects are used in conjunction with lots of hashtables and I was thinking, that...
12
by: Rubbrecht Philippe | last post by:
Hi there, According to documentation I read the ArrayList.IndexOf method uses the Object.Equals method to loop through the items in its list and locate the first index of an item that returns...
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...
10
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.