我有一个场景,我可以使用NameValueCollection或IDictionary。但我想知道哪一个更好的表现。IDictionary <string,string>或NameValueCollection
- 使用的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还支持每个键的多个值(查询字符串等所需)。 – 2009-03-06 04:37:00
不正确。根据这个有关namevaluecollection的msdn文章:http://msdn.microsoft.com/en-us/library/system.collections.specialized.namevaluecollection.aspx “这种类型的集合不保留元素的排序,并且不枚举集合时特定的顺序是有保证的。“ – kateroh 2011-03-04 18:38:42