NameValueCollection 与 Dictionary

发布于 2024-09-04 19:05:43 字数 1124 浏览 5 评论 0原文

可能的重复:
IDictionary;或NameValueCollection

我应该使用 Dictionary的任何原因;而不是 NameValueCollection?

(在 C# / .NET Framework 中)

选项 1,使用 NameValueCollection:

//enter values:
NameValueCollection nvc = new NameValueCollection()
{
  {"key1", "value1"},
  {"key2", "value2"},
  {"key3", "value3"}
};

// retrieve values:
foreach(string key in nvc.AllKeys)
{
  string value = nvc[key];
  // do something
}

选项 2,使用 Dictionary...

//enter values:
Dictionary<string, string> dict = new Dictionary<string, string>()
{
  {"key1", "value1"},
  {"key2", "value2"},
  {"key3", "value3"}
};

// retrieve values:
foreach (KeyValuePair<string, string> kvp in dict)
{
  string key = kvp.Key;
  string val = kvp.Value;
  // do something
}

对于这些用例,使用其中一种与另一种相比是否有任何优势?在性能、内存使用、排序顺序等方面有什么区别吗?

Possible Duplicate:
IDictionary<string, string> or NameValueCollection

Any reason I should use Dictionary<string,string> instead of NameValueCollection?

(in C# / .NET Framework)

Option 1, using NameValueCollection:

//enter values:
NameValueCollection nvc = new NameValueCollection()
{
  {"key1", "value1"},
  {"key2", "value2"},
  {"key3", "value3"}
};

// retrieve values:
foreach(string key in nvc.AllKeys)
{
  string value = nvc[key];
  // do something
}

Option 2, using Dictionary<string,string>...

//enter values:
Dictionary<string, string> dict = new Dictionary<string, string>()
{
  {"key1", "value1"},
  {"key2", "value2"},
  {"key3", "value3"}
};

// retrieve values:
foreach (KeyValuePair<string, string> kvp in dict)
{
  string key = kvp.Key;
  string val = kvp.Value;
  // do something
}

For these use cases, is there any advantage to use one versus the other? Any difference in performance, memory use, sort order, etc.?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若水微香 2024-09-11 19:05:43

它们在语义上并不相同。 NameValueCollection 可以有重复的键,而 Dictionary 则不能。

就我个人而言,如果您没有重复的键,那么我会坚持使用 Dictionary。它更现代,使用 IEnumerable<>,这使得与 Linq 查询混合起来很容易。您甚至可以使用 Linq ToDictionary() 方法创建一个 Dictionary

They aren't semantically identical. The NameValueCollection can have duplicate keys while the Dictionary cannot.

Personally if you don't have duplicate keys, then I would stick with the Dictionary. It's more modern, uses IEnumerable<> which makes it easy to mingle with Linq queries. You can even create a Dictionary using the Linq ToDictionary() method.

随心而道 2024-09-11 19:05:43

NameValueCollectionstring 类型,而 Dictionary 利用泛型来允许类型变化。请参阅泛型的优点

NameValueCollection is string typed whereas Dictionary leverages generics to allow type variance. See Benefits of Generics.

傲影 2024-09-11 19:05:43

Dictionary 会快得多。 NameValueCollection 允许重复键。这在某些情况下可能是不好的,但在其他情况下可能是理想的。 字典不允许重复的键。

来自: http://msdn.microsoft.com/en-us/library/ xfhwa508.aspx

字典<(Of <(TKey, TValue>)>)
泛型类提供了一个映射
一组键对应一组值。每个
除了字典之外,还包括
一个值及其关联的键。
使用键检索值是
非常快,接近O(1),因为
字典<(Of <(TKey, TValue>)>)
类被实现为哈希表。

Dictionary will be much faster. NameValueCollection allows duplicate keys. Which could be bad in certain situations, or desired in other. Dictionary does not allow duplicate keys.

From: http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

The Dictionary<(Of <(TKey, TValue>)>)
generic class provides a mapping from
a set of keys to a set of values. Each
addition to the dictionary consists of
a value and its associated key.
Retrieving a value by using its key is
very fast, close to O(1), because the
Dictionary<(Of <(TKey, TValue>)>)
class is implemented as a hash table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文