I want to use a generic collection like Dictionary
, but Dictionary
requires that every key be unique. I have multiple values for the same "key", so I need a generic collection that will allow for that.
I realize that this makes the key no longer really a key, but I don't know what else to call it.
Several options for you to consider:
Dictionary<TKey, List<TValue>>
— keep a list of values for each key, not preventing duplicate values for the same key (i.e. duplicate pairs);Dictionary<TKey, HashSet<TValue>>
— keep a set of value for each key, preventing duplicate values for the same key;List<KeyValuePair<TKey, TValue>>
— keep a list of pair, not preventing duplicate values for the same key.Note that in the latter case KeyValuePair
is a struct
, not a class
, hence that implies a bit different handling.
The right option depends on your actual use case.