20

I have a dictionary which is keyed by a List:

private Dictionary<List<custom_obj>, string> Lookup; 

I'm trying to use ContainsKey, but it doesn't seem to be working, and I have no idea why. Here is the debug information from my Visual Studio Immediate Window:

?Lookup.Keys.ElementAt(7)[0] {custom_obj} Direction: Down SID: 2540 ?Lookup.Keys.ElementAt(7)[1] {custom_obj} Direction: Down SID: 2550 searchObject[0] {custom_obj} Direction: Down SID: 2540 searchObject[1] {custom_obj} Direction: Down SID: 2550 ?Lookup.ContainsKey(searchObject) false 

In my common sense, that last ContainsKey should be true. Hopefully I've included enough information here... any ideas?

Thanks!

4 Answers 4

18

The List<custom_obj> instance acting as a key is referentially unequal to the instance referred to by searchObject.

If you want the dictionary to use the values in the list instead of referential equality to find matching keys, you must supply an IEqualityComparer in the constructor of the dictionary (since you can't override Equals and GetHashCode in List<T>).

Sign up to request clarification or add additional context in comments.

Comments

14

You have two separate Lists that contain the same elements. The correct way to find out if two lists are equal is with the SequenceEqual method.

You cannot by default do what you are trying to do. You can however, write a custom IEqualityComparer and pass it into the Dictionary constructor.

Here is a sample generic IEqualityComparer:

class ListComparer<T> : IEqualityComparer<List<T>> { public bool Equals(List<T> x, List<T> y) { return x.SequenceEqual(y); } public int GetHashCode(List<T> obj) { int hashcode = 0; foreach (T t in obj) { hashcode ^= t.GetHashCode(); } return hashcode; } } 

You may want to improve on the GetHashCode implementation, as this was a quick-and-dirty solution.

4 Comments

GetHashCode is missing a return.
Quick note: As the answer hints at, you should not use this Comparer. The equals method is sensitive to order but the hash method is not.
You can use StructuralComparisons class (since .NET 4.0 and Visual Studio 2010), see this answer.
Starting from .NET Core 2.1 or .NET Framework 4.6.1, you can use System.HashCode, which might be better than implementing your own hash. See this answer.
3

This will only work if the actual list instance used in the lookup is the same as the instance that was added as a key. It will not compare the list contents. This is the same behavior you will get if you try to compare two List objects directly.

Comments

0

Are you certain that the instance you are using in your lookup method is the same instance that is among your dictionary's keys? That is the only thing I can think of.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.