Re: override GetHashCode
From: Bart De Boeck (bart)
Date: 03/09/05
- Next message: Josef Meile: "Calling constructor override after doing some checks"
- Previous message: Glenn: "Re: How to make application wait some time ?"
- In reply to: Stoyan: "Re: override GetHashCode"
- Next in thread: Helge Jensen: "Re: override GetHashCode"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 9 Mar 2005 16:41:47 +0100
as far as I know, one of the reasons that you should override Equals is that
might require that different objects (different references of the same type)
can be equal
so, based on your example :
public class SomeType
{
public override int GetHashCode()
{ return 1;}
int _field = 3;
}
if two instances of SomeType are equal (cause _field equals), you should
override Equals
-- http://www.xenopz.com "Stoyan" <Stoyan@discussions.microsoft.com> wrote in message news:5B42AECF-66EF-4EED-A6AA-FC30B280AE7F@microsoft.com... > Hi Bart, > I understand very well, if I override Equals, I must override and > GetHashCode() method. > but I don't understand for example: > I have two classes: > public class SomeType > { > public override int GetHashCode() > { return 1;} > } > public class AnotherType > { > public override int GetHashCode() > { return 1;} > } > > SomeType a = new SomeType(); > AnotherType b = new AnotherType(); > Hashtable tbl = new Hashtable(); > tbl.Add(a,a); > tbl.Add(b,b); > > > object a = tbl[a]; // this object is correct (SomeType) > object b = tbl[b]; // this object is correct (AnotherType) > > In my case, why must also override Equals()? > I know, if hashcodes are different, Hashtable working better(faster) > > Regards, > Stoyan > > "Bart De Boeck" wrote: > >> Hi Stoyan, >> >> The Hashtable type requires that if two objects are equal, they must >> have >> the same hash code value : when a key/value pair is added to a Hashtable, >> the Hashtable uses the hash code of the key to determine the bucket that >> stores the key. The bucket is then searched for a key which Equals the >> given key (there might be more than one key in the bucket). If you wonder >> why Equals is needed to find the key in the bucket : hashes can collide >> (i.e. two different objects might generate the same hash - a hash >> collision). >> http://samples.gotdotnet.com/quickstart/howto/doc/hashtable.aspx >> >> Regards, >> Bart >> >> -- >> http://www.xenopz.com >> "Stoyan" <Stoyan@discussions.microsoft.com> wrote in message >> news:FF1EE5CC-0E92-4A30-81C9-B8430C929795@microsoft.com... >> > Hi All, >> > I don't understand very well this part of MSDN: >> > "Derived classes that override GetHashCode must also override Equals to >> > guarantee that two objects considered equal have the same hash code; >> > otherwise, Hashtable might not work correctly." >> > Does any one know, why we must also override Equals, >> > Please give my an example:) >> > >> > Thanks, >> > Stoyan >> >> >>
- Next message: Josef Meile: "Calling constructor override after doing some checks"
- Previous message: Glenn: "Re: How to make application wait some time ?"
- In reply to: Stoyan: "Re: override GetHashCode"
- Next in thread: Helge Jensen: "Re: override GetHashCode"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|