What is the best way to create your own GetHashCode method for a class in C#? Suppose I have a simple class (which overrides the Equals method), as follows:
class Test
{
public string[] names;
public double[] values;
public override bool Equals(object obj)
{
return (obj is Test) && this.Equals((Test)obj);
}
public bool Equals(Test t)
{
return names.Equals(t.names) && values.Equals(t.values);
}
}
Should I use the default code for the GetHashCode method?
public override int GetHashCode()
{
return base.GetHashCode();
}
Should I base the method on the contents of my class?
public override int GetHashCode()
{
return names.GetHashCode() + values.GetHashCode() ;
}
Or should I do something else?
System.Arraydoes not overrideGetHashCodeorEquals, so they use reference equality. Therefore, you shouldn’t call them.To implement
GetHashCode, see this question.To implement
Equals, use theSequenceEqualextension method.EDIT: On .Net 2.0, you’ll have to write your own version of
SequenceEqual, like this:You could write it to take
IEnumerable<T>instead ofIList<T>, but it’d be somewhat slower because it wouldn’t be able to exit early if the parameters have different sizes.