C# List as Dictionary key

2019-04-03 18:56发布

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条回答
Summer. ? 凉城
2楼-- · 2019-04-03 19:29

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.

查看更多
Root(大扎)
3楼-- · 2019-04-03 19:35

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.

查看更多
Evening l夕情丶
4楼-- · 2019-04-03 19:42

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>).

查看更多
我想做一个坏孩纸
5楼-- · 2019-04-03 19:43

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.

查看更多
登录 后发表回答