I've got a need to have a dictionary that returns more than one object
for a given key. There are a few ways of doing this that I could
think of, but they weren't really elegant and/or reusable:
1) Build a custom container or extended class containing the two
objects, then do a regular Dictionary<KeyType, Container>.
2) Have a Dictionary<KeyType, KeyValuePair<ValueType1, ValueType2>>.
I had an idea for something new, but I don't know if this is
necessarily a good idea, or if it's been done before - Please let me
know if you have an opinion on this:
class Trictionary<TKey, TValue1, TValue2>
It would contain a private Container<TValue1, TValue2> class (that
stores the two values) and a Dictionary<TKey, Container>, both behind
the scenes.
It would have methods like the following - basically wrappers around
the private Dictionary:
Set(TKey key, TValue1 value1, TValue2 value2) {}
Remove(TKey key) {}
Clear() {}
ContainsKey(TKey key) {}
Get(TKey key, out TValue1 value1, out TValue2 value2) {}
I don't like the idea of "out" parameters, but I couldn't think of any
other way to return two values, without resorting to something like an
F# Tuple.
You could iterate and get a series of KeyDualValuePair<TKey, TValue1,
TValue2> objects (a custom object, instead of the normal KeyValuePair
that you could get by iterating a Dictionary).
Does anyone have any experience doing something like this? If you
have any ideas or insight into this technique, I'd appreciate it.
Thanks
Joe