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.
Although I am late to the game, .NET Framework 4.5 provides new classes for you. See SortedList<TKey, TValue>
or SortedDictionary<TKey, TValue>
. If you are wondering which one you should use, the MSDN provides a few good reasons why you might choose one over the other.
The SortedList generic class is an array of key/value pairs with O(log n) retrieval, where n is the number of elements in the dictionary. In this, it is similar to the SortedDictionary generic class. The two classes have similar object models, and both have O(log n) retrieval. Where the two classes differ is in memory use and speed of insertion and removal:
SortedList<TKey, TValue>
uses less memory thanSortedDictionary<TKey, TValue>
.SortedDictionary<TKey, TValue>
has faster insertion and removal operations for unsorted data, O(log n) as opposed to O(n) forSortedList<TKey, TValue>
.- If the list is populated all at once from sorted data,
SortedList<TKey, TValue>
is faster thanSortedDictionary<TKey, TValue>
.Another difference between the
SortedDictionary<TKey, TValue>
andSortedList<TKey, TValue>
classes is thatSortedList<TKey, TValue>
supports efficient indexed retrieval of keys and values through the collections returned by the Keys and Values properties. It is not necessary to regenerate the lists when the properties are accessed, because the lists are just wrappers for the internal arrays of keys and values.
Both links have similar Remarks section (which is where the quote comes from). They also have more information for both classes. I'd recommend reading both sections if you are interested in using one of them.
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.
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){
...}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With