Object.GetHashCode()

G

Guinness Mann

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;
}

}
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
R

Ray Hsieh (Ray Djajadinata)

-----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-----
 
G

Guinness Mann

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
 
R

Ray Hsieh (Ray Djajadinata)

-----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 <[email protected]>,
| (e-mail address removed) 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+nByWGWwzLPACfb49+
/WbS5gbPdMissPWsOe/N168=
=AvxA
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top