Re: Own implementation of GetHashCode()

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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


.



Relevant Pages