问题
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!
回答1:
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>
).
回答2:
You have two separate List
s 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.
回答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.
回答4:
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.
来源:https://stackoverflow.com/questions/10020541/c-sharp-list-as-dictionary-key