Re: Own implementation of GetHashCode()
- From: "Dennis Myrén" <dennis@xxxxxxxxx>
- Date: Wed, 20 Apr 2005 15:46:34 +0100
There are probably many ways, but this is one of them.
You could concatenate str1 and str2 and then use that hash code, as of the
System.String.GetHashCode implementation.
public override int GetHashCode()
{
return string.Concat(str1, str2).GetHashCode();
}
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Matthias Kientz" <nospam_kientz@xxxxxxxxxxxxx> wrote in message
news:%23PgmMbbRFHA.2788@xxxxxxxxxxxxxxxxxxxxxxx
>I have a class which should override the GetHashcode() function, which look
>like this:
>
> public class A
> {
> public string str1;
> public string str2;
>
> //other methods...
>
> public override int GetHashCode()
> {
> return ???;
> }
> }
>
> The instance members str1 and str2 are the "primary keys" of my class.
> All instances of A with the same values for str1 and str2 should return
> the same hash code. The implementation should be also as fast as possible.
>
> An instance with A.str1 = "x" and A.str2 = "y" should return a different
> hashcode as an instance with A.str1 = "y" and A.str2 = "x".
>
> How to implement such a behavior and ensure that my hash code is
> consistent? How does the implementation of System.String do this?
>
> Thanks for any help and suggestions.
> Matthias
.
- Follow-Ups:
- Re: Own implementation of GetHashCode()
- From: Matthias Kientz
- Re: Own implementation of GetHashCode()
- References:
- Own implementation of GetHashCode()
- From: Matthias Kientz
- Own implementation of GetHashCode()
- Prev by Date: generics and reflection
- Next by Date: Suggestions for further reading...
- Previous by thread: Own implementation of GetHashCode()
- Next by thread: Re: Own implementation of GetHashCode()
- Index(es):
Relevant Pages
|