18

Using this class

public class Foo
{
    public string c1, c2;

    public Foo(string one, string two)
    {
        c1 = one;
        c2 = two;
    }

    public override int GetHashCode()
    {
        return (c1 + c2).GetHashCode();
    }
}

And this HashSet

HashSet<Foo> aFoos = new HashSet<Foo>();
Foo aFoo = new Foo("a", "b");

aFoos.Add(aFoo);
aFoos.Add(new Foo("a", "b"));

label1.Text = aFoos.Count().ToString();

I get the answer 2, when surely it should be 1. Is there a way to fix this so my HashSet contains only unique objects?

Thanks, Ash.

Ash
  • 3,279
  • 4
  • 28
  • 26

3 Answers3

29

The HashSet<T> type ultamitely uses equality to determine whether 2 objects are equal or not. In the type Foo you have only overridden GetHashCode and not equality. This means equality checks will default back to Object.Equals which uses reference equality. This explains why you see multiple items in the HashSet<Foo>.

To fix this you will need to override Equals in the Foo type.

public override bool Equals(object obj) { 
  var other = obj as Foo;
  if (other == null) {
    return false;
  }
  return c1 == other.c1 && c2 == other.c2;
}
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 3
    +1 because it is correct, but one may add that the hashes returned by `GetHashCode` are allowed to collide for different values, which is why comparing the hash code does not ensure the equality of objects in general. – Lucero Jan 04 '11 at 21:59
7

You need to override Equals method. Only GetHashCode is not enough.

Victor Haydin
  • 3,518
  • 2
  • 26
  • 41
4

You need to override the equals method as well. The reason for this is that the hashcode is allowed to collide for two objects that is not equal. Otherwise it won't work.

public override bool Equals(Object obj)
{ 
   Foo otherObject = obj as Foo;
   return otherObject != null && otherObject.c1 == this.c1 && otherObject.c2 == this.c2;
}
Tomas Jansson
  • 22,767
  • 13
  • 83
  • 137