C# 有序字典和字典

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16694182/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:41:25  来源:igfitidea点击:

OrderedDictionary and Dictionary

c#dictionaryordereddictionary

提问by DeadlyJesus

I was looking for a way to have my Dictionaryenumerate its KeyValuePairin the same order that they were added. Now, Dictionary's documentationclearly states that:

我正在寻找一种方法让我按照添加它们的相同顺序Dictionary枚举它KeyValuePair。现在,字典的文档明确指出:

For purposes of enumeration, each item in the dictionary is treated as a KeyValuePair<TKey, TValue>structure representing a value and its key. The order in which the items are returned is undefined.

出于枚举的目的,字典中的每一项都被视为KeyValuePair<TKey, TValue>表示值及其键的结构。项目返回的顺序未定义。

I found out that what I needed was an OrderedDictionary, but being the sceptic that I am, I decided to try it myself:

我发现我需要的是一个OrderedDictionary,但作为我的怀疑论者,我决定自己尝试一下:

OrderedDictionary od = new OrderedDictionary();
Dictionary<String, String> d = new Dictionary<String, String>();

for (int i = 0; i < 10; i++)
{
    od.Add("key" + i, "value" + i);
    d.Add("key" + i, "value" + i);
}

System.Console.WriteLine("OrderedDictionary");
foreach (DictionaryEntry de in od) {
    System.Console.WriteLine(de.Key + ", " + de.Value);
}

System.Console.WriteLine("Dictionary");
foreach (var tmp in d) {
    System.Console.WriteLine(tmp.Key + ", " + tmp.Value);
}

Output:

输出:

OrderedDictionary
key0, value0
key1, value1
key2, value2
...

Dictionary
key0, value0
key1, value1
key2, value2
...

As you can see, both are ordered, and that raise two questions:

如您所见,两者都是有序的,这就提出了两个问题:

In which case does the Dictionarygive a different order that the one in which the values are added? Does my first foreachloop assure me to retrieve my KeyValuePairin the same order, or do I have to use the index?

在哪种情况下,Dictionary给出的顺序与添加值的顺序不同?我的第一个foreach循环是否确保我以KeyValuePair相同的顺序检索我的,还是必须使用索引?

采纳答案by Ilya Ivanov

You are doing it wrong. You need not only to insert values sequentially into dictionary, but also remove some elements and see how the order has changed after this. The next code demonstrates this:

你做错了。您不仅需要按顺序将值插入字典中,还需要删除一些元素并查看此后顺序如何更改。下一个代码演示了这一点:

OrderedDictionary od = new OrderedDictionary();
Dictionary<String, String> d = new Dictionary<String, String>();
Random r = new Random();

for (int i = 0; i < 10; i++)
{
    od.Add("key" + i, "value" + i);
    d.Add("key" + i, "value" + i);
    if (i % 3 == 0)
    {
        od.Remove("key" + r.Next(d.Count));
        d.Remove("key" + r.Next(d.Count));
    }
}

System.Console.WriteLine("OrderedDictionary");
foreach (DictionaryEntry de in od) {
    System.Console.WriteLine(de.Key + ", " +de.Value);
}

System.Console.WriteLine("Dictionary");
foreach (var tmp in d) {
    System.Console.WriteLine(tmp.Key + ", " + tmp.Value);
}

prints something similar to (OrderedDictionary is always ordered):

打印类似于(OrderedDictionary 总是有序的):

OrderedDictionary
key3, value3
key5, value5
key6, value6
key7, value7
key8, value8
key9, value9
Dictionary
key7, value7
key4, value4
key3, value3
key5, value5
key6, value6
key8, value8
key9, value9