我有一个场景,其中我可以使用 NameValueCollection 或 IDictionary。但我想知道哪一个在性能方面会更好。
-- 使用 NameValueCollection
NameValueCollection options()
{
NameValueCollection nc = new NameValueCollection();
nc = ....; //populate nc here
if(sorting)
//sort NameValueCollection nc here
return nc;
}
-- 使用 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;
}
最佳答案
这些集合类型不能完全互换:NameValueCollection
允许通过整数索引 访问。如果您不需要该功能,则不应使用 NameValueCollection
因为索引不是“免费”。
根据您正在查看的字符串数量,我会考虑 Hashtable<string, string>
或 IDictionary<string, string>
. Krzysztof Cwalina 在这里讨论了微妙之处:http://blogs.gotdotnet.com/kcwalina/archive/2004/08/06/210297.aspx .
关于c# - IDictionary<string, string> 或 NameValueCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/617443/