Reputation: 4982
I need a Dictionary whose key is an array of integers for example Dictionary<int[],string>
or
Dictionary<List<int>,string>.
But I am quite surprised that the Equality method and hash code method is not defined for me. Is there any easy way to implement such a structure other than creating my own MyType: List<int>
and to define all necessary methods?
Upvotes: 2
Views: 6994
Reputation: 942438
It isn't predefined because it is expensive. If you know your list is short then just implement the obvious overrides. If not, you'll have to come up with some kind of heuristic for at least GetHashCode. Say, GetHashCode of only the first couple of elements xor-ed together with the Length.
Upvotes: 3
Reputation: 20054
Instead of creating your own type, you could provide two methods somewhere
string ConvertListToString(List<int> l){...};
List<int> ConvertStringToList(string s){...};
and use a Dictionary<string,string>
instead.
Upvotes: 1
Reputation: 46903
GetHashCode and Equality are defined for List, they're just not overridden to give you behavior that you might expect and instead.
If you're using .NET 3.5 you can write a extension methods for List that implements an override for both GetHashCode()
, and Equality()
Upvotes: 0