Re: override GetHashCode

From: Bart De Boeck (bart)
Date: 03/09/05


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


Relevant Pages

  • RE: Equality vs Sameness
    ... Equals would not offer any benefit. ... >> from it in the future if the users haven't asked for that functionality. ... So you override Equals and GetHashCode even for forms, ... or wanted to compare two instances of a singleton? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: comparing objects
    ... I override the Equal method on SalesRegion ... As Arne already put it, the Contains method is using the Equals method, ... ArrayList salesRegions = ... private string s; ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Find object inside arraylists by object attribute
    ... you will need to override the Equals method inside your class such that it ... public override int GetHashCode() ... instead that you just use that in your arraylit instead: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Find object inside arraylists by object attribute
    ... > public override bool Equals ... > //whenever you override equals you should override GetHashCode too ... > public override int GetHashCode() ... > instead that you just use that in your arraylit instead: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Appropriate GetHashCode() override?
    ... Equals(), so I need to override GetHashCode(). ... I mean, literally speaking the _reference_ to the instance is a unique identifier, but if that were really sufficient for your needs, you wouldn't have to override Equalsat all. ... It is true that you must _not_ use that single field for your hash code if other fields are included in the Equalstest, but the very fact that you're asking about this means that you haven't fully resolved what it means for two instances of your class to be equal. ...
    (microsoft.public.dotnet.languages.csharp)