I have a scenario where-in I can use either NameValueCollection or IDictionary. But I would like to know which one will be better performance-wise.
-- Using NameValueCollection
NameValueCollection options() { NameValueCollection nc = new NameValueCollection(); nc = ....; //populate nc here if(sorting) //sort NameValueCollection nc here return nc; }
-- using IDictionary
IDictionary<string, string> options() { Dictionary<string, string> optionDictionary = new Dictionary<string, string>(); optionDictionary = ....; //populate if(sorting) return new SortedDictionary<string, string>(optionDictionary); else return optionDictionary; }
These collection types are not exactly interchangeable: NameValueCollection
allows access via integer indexes. If you don't need that functionality, you shouldn't use a NameValueCollection
as indexing doesn't come "for free".
Depending on the number of strings you're looking at, I would consider either Hashtable<string, string>
or IDictionary<string, string>
. Krzysztof Cwalina discusses the subtleties here: http://blogs.gotdotnet.com/kcwalina/archive/2004/08/06/210297.aspx.
The other advantage of IDictionary is that it's not implementation specific unlike NameValueCollection.
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