I've been looking for a way to store and retrieve values on more than the single key that C#'s generic Dictionary class provides.
Searching around the web (and on SO itself) has shown me a couple options:
Tuple Based Dictionaries
.NET 4.0 makes it easy to support a generic Tuple<,> class. This means you can make a Dictionary out of any arbitrary Tuple, i.e.,
var myDict = new Dictionary<Tuple<Char, Int>, MyClass>();
Nested Dictionaries
I've learned you can also nest Dictionaries within Dictionaries, which makes accessing the stored result similar to accessing an N-Dimensional array. For instance:
Dictionary<int, Dictionary<int, Dictionary<Char, MyClass>>>
which could then be accsessed by: MyClass foo = MyData[8][3]['W'];
Delimited Concatenated Key Dictionaries
But while both work well for complex data and custom classes, I wonder if they're always necessary. For primitive data, at least, it would seem that concatenating the keys with a delimiter is just as effective.
//keys are char + int
Dictionary<string, MyClass> myDict = New Dictionary<string, Myclass>();
String input = myChar + "|" + myInt
MyClass foo = myDict[input]
Are there any scenarios which make one of these methods superior to the other? Will they have similar performance times? Or should the focus be instead on which method provides the cleanest, easiest to maintain, code?
Thoughts?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…