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

Object.GetHashCode()

resource.cs(8,15): warning CS0659: 'UA.LMS.Resource.Resource' overrides
Object.Equals(object o) but does not override Object.GetHashCode()

What does this mean? I mean I know what it says, but do I need to do
anything? If I specify exactly how to do the comparison why would I
need to provide a hashcode override?

Here are the interesting pieces of my class:

public class Resource
{
public int rId;
public string language;
public string caption;

(Various constructors elided)

public static bool operator==(Resource lhs, Resource rhs)
{
if
( (lhs.rId == rhs.rId)
&& (lhs.language == rhs.language)
&& (lhs.caption == rhs.caption)
)
{
return true;
}

return false;
}

public static bool operator !=(Resource lhs, Resource rhs)
{
return !(lhs == rhs);
}

public override bool Equals(object obj)
{
if (! (obj is Resource))
return false;

return this == (Resource)obj;
}

}
Nov 15 '05 #1
4 14901
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Because if you override Equals() but not GetHashCode(), that means you
can have two equal objects, with two different hashcode. Now that's bad! :)

Guinness Mann wrote:

| resource.cs(8,15): warning CS0659: 'UA.LMS.Resource.Resource' overrides
| Object.Equals(object o) but does not override Object.GetHashCode()
|
| What does this mean? I mean I know what it says, but do I need to do
| anything? If I specify exactly how to do the comparison why would I
| need to provide a hashcode override?
|
| Here are the interesting pieces of my class:
|
| public class Resource
| {
| public int rId;
| public string language;
| public string caption;
|
| (Various constructors elided)
|
| public static bool operator==(Resource lhs, Resource rhs)
| {
| if
| ( (lhs.rId == rhs.rId)
| && (lhs.language == rhs.language)
| && (lhs.caption == rhs.caption)
| )
| {
| return true;
| }
|
| return false;
| }
|
| public static bool operator !=(Resource lhs, Resource rhs)
| {
| return !(lhs == rhs);
| }
|
| public override bool Equals(object obj)
| {
| if (! (obj is Resource))
| return false;
|
| return this == (Resource)obj;
| }
|
| }
- --
Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
ray underscore usenet at yahoo dot com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQE/oVNWwEwccQ4rWPgRAuW3AJ98MVeZtV9DQwqWRLmqt0V/4jpvOACeMAUo
4LBH2gqFoHd9tK5WmeDhcwE=
=UPTy
-----END PGP SIGNATURE-----

Nov 15 '05 #2
Ray and Guiness Mann,

Furthermore, if your object is used as a key in a hashtable, then you
will have two instances of the object, which are considered equal, filling
two separate slots in the hashtable. This would be bad as well.

When overriding GetHashCode, you can just get the hashcode of the three
items you are comparing, and XOR all of them together, and that should give
you a hashcode that you can return.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Ray Hsieh (Ray Djajadinata)" <ch***@my.signature.com> wrote in message
news:OK**************@TK2MSFTNGP12.phx.gbl...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Because if you override Equals() but not GetHashCode(), that means you
can have two equal objects, with two different hashcode. Now that's bad! :)
Guinness Mann wrote:

| resource.cs(8,15): warning CS0659: 'UA.LMS.Resource.Resource' overrides
| Object.Equals(object o) but does not override Object.GetHashCode()
|
| What does this mean? I mean I know what it says, but do I need to do
| anything? If I specify exactly how to do the comparison why would I
| need to provide a hashcode override?
|
| Here are the interesting pieces of my class:
|
| public class Resource
| {
| public int rId;
| public string language;
| public string caption;
|
| (Various constructors elided)
|
| public static bool operator==(Resource lhs, Resource rhs)
| {
| if
| ( (lhs.rId == rhs.rId)
| && (lhs.language == rhs.language)
| && (lhs.caption == rhs.caption)
| )
| {
| return true;
| }
|
| return false;
| }
|
| public static bool operator !=(Resource lhs, Resource rhs)
| {
| return !(lhs == rhs);
| }
|
| public override bool Equals(object obj)
| {
| if (! (obj is Resource))
| return false;
|
| return this == (Resource)obj;
| }
|
| }
- --
Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
ray underscore usenet at yahoo dot com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQE/oVNWwEwccQ4rWPgRAuW3AJ98MVeZtV9DQwqWRLmqt0V/4jpvOACeMAUo
4LBH2gqFoHd9tK5WmeDhcwE=
=UPTy
-----END PGP SIGNATURE-----

Nov 15 '05 #3
In article <Oz**************@TK2MSFTNGP11.phx.gbl>,
mv*@spam.guard.caspershouse.com says...
Ray and Guiness Mann,
When overriding GetHashCode, you can just get the hashcode of the three
items you are comparing, and XOR all of them together, and that should give
you a hashcode that you can return.


Ray and Nicholas,

Let's take a step back. Did I need to override object.Equals, anyway,
or can I just provide my own Resource.Equals?

-- Rick
Nov 15 '05 #4
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Rick, you need to override object.Equals(), *in* your Resource class.

Also, Nicholas, for the HashCode may I suggest this generic formula:

int result = 17;
result = 37*result + rId.GetHashCode();
result = 37*result + language.GetHashCode();
result = 37*result + caption.GetHashCode();
hashCode = result;

(17 and 37 are arbitrary--37 is chosen because it is prime.)

This way, the order of your field is taken into account. Consider this:

~ Resource res1 = new Resource(12, "en", "blah");
~ Resource res2 = new Resource(12, "blah", "en");

They are not equal, but if you XOR the fields to get the HashCode, they
will have identical hashcode. Granted, the example is contrived for this
particular Resource class, but you will encounter cases like this in
other classes, where XOR-ing the hashcodes of the fields is not sufficient.
Guinness Mann wrote:
| In article <Oz**************@TK2MSFTNGP11.phx.gbl>,
| mv*@spam.guard.caspershouse.com says...
| -- Rick
- --
Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
ray underscore usenet at yahoo dot com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQE/obbewEwccQ4rWPgRAjqoAJ0WQo71XzkuHTm6CT+nByWGWwzLPA Cfb49+
/WbS5gbPdMissPWsOe/N168=
=AvxA
-----END PGP SIGNATURE-----

Nov 15 '05 #5

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

Similar topics

1
by: Clemens Hoffmann | last post by:
Hello, i have a nasty problem with object identity in hash tables. I try to use different objects as keys. It failed bacause i cannot identify object propperly. Different objects with the same...
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: Fuzzy | last post by:
I have defined a struct in my application that contains 3 integers that maintain state information. I have a lot of these structs in time-critical portions of my app, so they must be as fast as...
2
by: One Handed Man [ OHM# ] | last post by:
The help for the .NET Framework Class Library tells us that the Object.GetHashCode() Method does not guarantee uniqueness' or consistency and that overriding this and the Equals method is a good...
6
by: Daniel Klein | last post by:
A simple question hopefully... Can the Object GetHashCode() method be used to obtain a unique identifier for an instance of an object? To put this another way, what does the...
1
by: MariusI | last post by:
I have some business objects which overrides Equals to provide syntax equality instead of just reference equality. Overriding equals gives me a warning that i must override GetHashcode(). Msdn says...
1
by: Stephan Keil | last post by:
Hi all, I am somewhat confused by the Object.GetHashCode() documentation. What I am looking for, is a hash code, which is bound to the _identity_ of an object, not to it's _value_ (see other...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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:
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...

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.