ListDictionary/NameValueCollection question

G

Guest

Hi
I have a situation wherein I need hashtable type functionality and at the
same time, the collection must preserve the order of the items.
In other words, if I add items

Key Value
"a" "Val - A"
"X" "Val - X"
"b" "Val - B"
When I enumerate over the collection, I should get items in the same order
as inserted. I read at some site that ListDictionary and NameValueCollection
support this. But I am not sure about the same. Can anyone confirm if I can
use any of these collections?

Thanks and regards
Vikas Manghani
 
J

Jon Skeet [C# MVP]

I have a situation wherein I need hashtable type functionality and at the
same time, the collection must preserve the order of the items.
In other words, if I add items

Key Value
"a" "Val - A"
"X" "Val - X"
"b" "Val - B"
When I enumerate over the collection, I should get items in the same order
as inserted. I read at some site that ListDictionary and NameValueCollection
support this. But I am not sure about the same. Can anyone confirm if I can
use any of these collections?

I don't believe either of those preserve insertion order. The easiest
way is to maintain both a List<T> and a Dictionary<K,V>. I'm sure
there are classes available on the net which do this for you, of
course, but in many cases it's easy just to keep both (the list being
a list of keys) and update it yourself.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top