Is There An Indexed Generic Key-Value Pair List In C#?

I need something like “Dictionary”, but i also need indexes of key-value pairs.

Note: OrderedDictionary does exactly what i want, but it’s not generic.

Just use a list of keyvaluepairs:

List<KeyValuePair<sometype, sometype>> myList = new List<KeyValuePair<sometype, sometype>>();

That being said, I strongly advise you to sit back and ask yourself if you would be better off codewise doing something like:

// could also be a class if you don't want it immutable
struct myStruct {
    int a;
    int b;

List<myStruct> myList = new List<myStruct>;

The bonus to doing it this way is that if you find later on you need to keep track of a third value, it’ll be easier to expand upon.


If you need to be able to access by key AND by index as well, and this isn’t just some sort of organization issue; You could roll out your own version of this. Just make a new container using the correct interfaces. Internally, keep track of a List, and a Dictionary, and code it in such a way that it has the behavior you want.

Sometime you just need to roll your own containers. I’ve had to do this for things like a UniqueList, DropoutStack, etc.