Click here to Skip to main content
11,928,754 members (51,229 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked

Multi-key generic dictionary class for C#

, 20 May 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
MultiKeyDictionary is a C# class that wraps and extends the Generic Dictionary object provided by Microsoft in .NET 2.0 and above.

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.

Using 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.

// Adding 'Zero' to dictionary with primary int key of '0'
dictionary.Add(0, "Zero");
// Associating binary sub key of '0000' with primary int key of '0'
dictionary.Associate("0000", 0);

//Adding 'Three' to dictionary with primary
//int key of '3' and a binary sub key of '0011'");
dictionary.Add(3, "0011", "Three");

// Getting value for binary sub key 0000
val = dictionary["0000"]; // val will be Zero
// Getting value for int primary key 0
val = dictionary[0]; // val will be Zero

The code

Here is the code that I am using to do all this fancy stuff... feel free to steal it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Aron.Weiler
    /// <span class="code-SummaryComment"><summary>


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Aron Weiler
Technical Lead CareFusion
United States United States
I just looooove software.

Check out my technical blog here: The Fyslexic Duck. You can find most of what I've put on CodeProject there, plus some additional technical articles.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 4 Pin
arpitgold21-May-11 7:50
memberarpitgold21-May-11 7:50 
GeneralMy vote of 3 Pin
Kamal Kant Joshi21-May-11 6:30
memberKamal Kant Joshi21-May-11 6:30 
GeneralInteresting ! ... and a minor suggestion Pin
BillWoodruff20-May-11 19:56
memberBillWoodruff20-May-11 19:56 
GeneralRe: Interesting ! ... and a minor suggestion Pin
Aron Weiler23-Jun-11 21:03
memberAron Weiler23-Jun-11 21:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 20 May 2011
Article Copyright 2011 by Aron Weiler
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid