Using the field of an object as a generic Dictionary key

Dana picture Dana · Mar 11, 2009 · Viewed 94.6k times · Source

If I want to use objects as the keys for a Dictionary, what methods will I need to override to make them compare in a specific way?

Say I have a a class which has properties:

class Foo {
    public string Name { get; set; }
    public int FooID { get; set; }

    // elided
} 

And I want to create a:

Dictionary<Foo, List<Stuff>>

I want Foo objects with the same FooID to be considered the same group. Which methods will I need to override in the Foo class?

To summarize: I want to categorize Stuff objects into lists, grouped by Foo objects. Stuff objects will have a FooID to link them to their category.

Answer

Marc Gravell picture Marc Gravell · Mar 11, 2009

By default, the two important methods are GetHashCode() and Equals(). It is important that if two things are equal (Equals() returns true), that they have the same hash-code. For example, you might "return FooID;" as the GetHashCode() if you want that as the match. You can also implement IEquatable<Foo>, but that is optional:

class Foo : IEquatable<Foo> {
    public string Name { get; set;}
    public int FooID {get; set;}

    public override int GetHashCode() {
        return FooID;
    }
    public override bool Equals(object obj) {
        return Equals(obj as Foo);
    }
    public bool Equals(Foo obj) {
        return obj != null && obj.FooID == this.FooID;
    }
}

Finally, another alternative is to provide an IEqualityComparer<T> to do the same.