C# IDictionary<string, string> 或 NameValueCollection
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/617443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IDictionary<string, string> or NameValueCollection
提问by Ujwala Khaire
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.
我有一个场景,我可以使用 NameValueCollection 或 IDictionary。但我想知道哪一个在性能方面会更好。
-- Using NameValueCollection
-- 使用 NameValueCollection
NameValueCollection options()
{
NameValueCollection nc = new NameValueCollection();
nc = ....; //populate nc here
if(sorting)
//sort NameValueCollection nc here
return nc;
}
-- using IDictionary
-- 使用 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;
}
回答by Justin R.
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".
这些集合类型不能完全互换:NameValueCollection
允许通过整数索引访问。如果您不需要该功能,则不应使用 aNameValueCollection
因为索引不是“免费的”。
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.
根据您正在查看的字符串数量,我会考虑Hashtable<string, string>
或IDictionary<string, string>
。Krzysztof Cwalina 在这里讨论了微妙之处:http: //blogs.gotdotnet.com/kcwalina/archive/2004/08/06/210297.aspx。
回答by lomaxx
The other advantage of IDictionary is that it's not implementation specific unlike NameValueCollection.
IDictionary 的另一个优点是与 NameValueCollection 不同,它不是特定于实现的。
回答by tsimon
I agree with fatcat and lomaxx (and up-voted for both answers). I would add that performance of collection types should most likely be the last consideration when choosing between collection types. Use the type that most fits your usage needs. If you are in a performance critical section of code (and most likely, you're not), then the only answer is to measure each case - don't believe the Interweb, believe the numbers.
我同意 fatcat 和 lomaxx(并且对这两个答案都投了赞成票)。我想补充一点,在集合类型之间进行选择时,集合类型的性能很可能是最后一个考虑因素。使用最适合您的使用需求的类型。如果您处于代码的性能关键部分(并且很可能不是),那么唯一的答案是衡量每个案例 - 不要相信互联网,相信数字。
回答by Shawn Kovac
A NameValueCollection in .NET is basically what's used for QueryStrings to hold key/value pairs. The biggest difference is when two items with the same key are added. With IDictionary, there are two ways to set a value. Using the .Add() method will throw an error on a duplicate key when the key already exists. But simply setting the item equal to a value will overwrite the value. This is how IDictionary handles duplicate keys. But NameValueCollection will add values like this: "value1,value2,value3". So the existing item value gets appended with a comma, then the new value appended to it each time.
.NET 中的 NameValueCollection 基本上是用于 QueryStrings 保存键/值对的内容。最大的区别是当添加两个具有相同键的项目时。使用 IDictionary,有两种设置值的方法。当键已经存在时,使用 .Add() 方法将在重复键上抛出错误。但简单地将项目设置为一个值将覆盖该值。这就是 IDictionary 处理重复键的方式。但是 NameValueCollection 会添加这样的值:“value1,value2,value3”。因此,现有项目值会附加一个逗号,然后每次都会附加新值。
It seems to me that this NameValueCollection was built specifically for QueryString use and access. A QueryString like "?a=1&b=2&a=3" in .NET will yield a result of item["a"] = "1,3". This difference of how duplicate keys are treated is the 'real' difference, that is, the biggest difference between the two.
在我看来,这个 NameValueCollection 是专门为 QueryString 使用和访问而构建的。在 .NET 中像 "?a=1&b=2&a=3" 这样的 QueryString 将产生 item["a"] = "1,3" 的结果。这种对重复键的处理方式的差异是“真正的”差异,即两者之间的最大差异。
I suspectthat a NameValueCollection also does not use any hash table for rapid access of keys when the collection is large because this access is slower for small collections than without the hash table. I have not found definitive information that states whether a NameValueCollection does or does not use a hash table to access the keys. I doknow that an IDictionary uses a hash table so that accessing keys in an IDictionary with many keys is quite fast. So i suspectthat a NameValueCollection is faster for small collections than an IDictionary. If my guess is correct, then it wud mean a NameValueCollection shud by no means be used for large collections since the larger it is, it massively slows down without a hash table to access the keys.
我怀疑当集合很大时 NameValueCollection 也不使用任何哈希表来快速访问键,因为对于小集合来说,这种访问比没有哈希表要慢。我还没有找到明确的信息来说明 NameValueCollection 是否使用哈希表来访问键。我确实知道 IDictionary 使用哈希表,因此访问具有许多键的 IDictionary 中的键非常快。所以我怀疑NameValueCollection 对于小集合来说比 IDictionary 更快。如果我的猜测是正确的,那么这意味着 NameValueCollection 绝不能用于大型集合,因为它越大,它在没有哈希表访问键的情况下会大大减慢。
For the number of keys in a querystring, this number is normally very small, so i wud guessthe NameValueCollection does notuse hashes, for better performance. But if Microsoft designed things for performance, and for what's best for their users, Windows wud be sodifferent than it is today. So we can't assume anything that 'shud be'.
对于在查询字符串键的数量,这个数字通常是非常小的,所以我WUD猜NameValueCollection中并没有使用哈希,获得更好的性能。但是,如果 Microsoft 设计的东西是为了性能,并且为了用户的最佳体验,那么Windows就会与今天大不相同。所以我们不能假设任何“应该是”的事情。
Also, i want to clarify an incorrect claim by the most popularly voted answer to this question. Kateroh's comment below the incorrect answer says it well enuf, that i don't need to add anything to it. But i repeat Kateroh's comment here so just maybe more people will realize that the most popular answer is wrong. Kateroh correctly states:
另外,我想通过对这个问题的最受欢迎投票的答案来澄清一个不正确的主张。Kateroh 在错误答案下方的评论说它很好,我不需要添加任何内容。但我在这里重复 Kateroh 的评论,所以也许更多的人会意识到最受欢迎的答案是错误的。卡特罗正确地指出:
- According to this msdn article about NameValueCollection: https://msdn.microsoft.com/en-us/library/system.collections.specialized.namevaluecollection.aspx"Collections of this type do not preserve the ordering of element, and no particular ordering is guaranteed when enumerating the collection." – kateroh Mar 4 '11 at 18:38
- 根据这篇关于 NameValueCollection 的 msdn 文章:https: //msdn.microsoft.com/en-us/library/system.collections.specialized.namevaluecollection.aspx“这种类型的集合不保留元素的顺序,也没有特定的顺序在枚举集合时保证。” – kateroh 11 年 3 月 4 日,18:38