By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,810 Members | 783 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,810 IT Pros & Developers. It's quick & easy.

Overriding Equals()

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.