views:

65

answers:

4

Is there an collection in .net that allows the storing KeyValuePair<string, string> that keeps the order of inserting?
OrderedDictionary looked promising, but seems to be rather lacking.
Now I'm looking into IOrderedEnumerable>, but I can't seem to find any implementation except for ISortedDictionary, but that's not what I want. No sorting needs to be done, just the order of inserting is important.

Update
The reason I don't like OrderedDictionary is that it's not generic.

+2  A: 

Just use List<KeyvaluePair<T,T>>. They are stored in order of insertion. Every time you add to it, the newest one is added to the end of the list.

so

var list = new List<KeyvaluePair<String,String>>();

list.Add(new KeyValuePair<String,String>("",""));

If you want to pull them out in order just use:

list.ForEach(x=>...);

or

foreach(var item in list){
...}
Kevin
Is it guarenteed that List keeps it order?
borisCallens
Yes, it has to, because you can access the elements directly just like you do an array List[0]..etc.
Kevin
+1  A: 

You might want to roll one using a Queue<T> where T is a KeyValuePair<string, string>. It's a more solid contract that explicitly guarantees insertion order.

http://msdn.microsoft.com/en-us/library/7977ey2c.aspx

Andy_Vulhop
A: 

You should just be able to use a List<KeyValuePair<string,string>>. I can't actually find in the MSDN documentation stating that the insertion order is guaranteed, but it's a pretty safe bet...

BFree
+2  A: 

OrderedDictionary is what you want if you need both keyed and insertion-sequenced access to items ... it's really just a combination of a hash table and a list. It provides a means to access items in it either by insertion index or by key. It's the only collection in .NET that does this. Sadly, it is not generic.

If OrderedDictionary doesn't meet your needs solely because it is not generic - then you can use the version here that provides a generic equivalent. If there are other reasons why it doesn't work for you, update your post and we can look for a better option.

While you can certainly create your own List<KeyValuePair<string,string>> you will lose the option of searching by key efficiently. Now, you can certainly roll your own implementation of an ordered doctionary that combined list/dict together ... but the post I've linked to already does this.

LBushkin
That sums up my options nicely. Since I won't be needing the key lookup anyway, I'm going with IList. Thanks
borisCallens