I implement a CaseAccentInsensitiveEqualityComparer for Strings. I do not know how to implement the GetHashCode

advertisements

My code is like this:

public class CaseAccentInsensitiveEqualityComparer : IEqualityComparer<string>
    {
        public bool Equals(string x, string y)
        {
            return string.Compare(x, y, CultureInfo.InvariantCulture, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase) == 0;
        }

        public int GetHashCode(string obj)
        {
             // not sure what to put here
        }
    }

I know the role of GetHashCode in this context, what I'm missing is how to produce the InvariantCulture, IgnoreNonSpace and IgnoreCase version of obj so that I can return it's HashCode.

I could remove diacritics and the case from obj myself and then return it's hashcode, but I wonder if there's a better alternative.


Ah, excuse me, I had my methods mixed up. When I implemented something like this before I just returned the hash code of the object itself return obj.GetHashCode(); so that it would always enter the Equals method.


Okay, after much confusion I believe I've got myself straight. I found that returning zero, always, will force the comparer to use the Equals method. I'm looking for the code I implemented this in to prove that and put it up here.


Here's the code to prove it.

class MyArrayComparer : EqualityComparer<object[]>
{
    public override bool Equals(object[] x, object[] y)
    {
        if (x.Length != y.Length) { return false; }
        for (int i = 0; i < x.Length; i++)
        {
            if (!x[i].Equals(y[i]))
            {
                return false;
            }
        }
        return true;
    }

    public override int GetHashCode(object[] obj)
    {
        return 0;
    }
}