MultiKeyDictionary is a C# class that wraps and extends the Generic
Dictionary object provided by Microsoft in .NET 2.0 and above. This allows a developer to create a generic dictionary of values and reference the value list through two keys instead of just the one provided by the Microsoft implementation of the Generic
Dictionary<...>. You can see my article on CodeProject (here), however this code is more up-to-date and bug free.
While writing a massive socket management application, I needed the ability to keep a list of sockets that I could identify by either their remote endpoint (
IPEndPoint) or by a string that represented the internal ID of the socket. Thus was born the
MultiKeyDictionary class is simple: instantiate the class, specifying the primary key, sub key, and value types in the generic constructor, then start adding your values and keys.
For this example, let's say I wanted to create a dictionary which stores the string representation of a number, i.e., 'Zero', 'One', 'Two', etc. Now, I want to access that list of items via an integer representation and a binary (in string format) representation.
dictionary.Add(3, "0011", "Three");
val = dictionary["0000"];
val = dictionary;
Here is the code that I am using to do all this fancy stuff... feel free to steal it.