为什么我不能在foreach中添加一个词典(Key,Value)?

时间:2021-11-06 20:39:00

If I want to loop over a Dictionary(Key, Value)... why cant I add a new key-value pair in the loop?

如果我想循环一个字典(键,值)...为什么我不能在循环中添加一个新的键值对?

Dictionary<string, string> persons = new Dictionary<string, string>();

persons.Add("roger", "12");
persons.Add("mary", "13");

foreach (KeyValuePair<string,string> person in persons)
{
 Console.WriteLine("Name: " + person.Key + ", Age: " + person.Value);
 persons.Add("Xico", "22");
}

8 个解决方案

#1


If you want to do this you should get a copy of the Dictionary Object (can be done in many ways) then enumerate it, add what you want to the original.

如果你想这样做,你应该得到一个字典对象的副本(可以通过多种方式完成),然后枚举它,添加你想要的原始对象。

Simply you can't add new items while enumerating an Dictionary.

在枚举字典时,您无法添加新项目。

#2


It's because of the way the enumerator is constructed.

这是因为构造枚举器的方式。

From MSDN Dictionary.Enumerator:

来自MSDN Dictionary.Enumerator:

An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying, or deleting elements, the enumerator is irrecoverably invalidated and its behavior is undefined.

只要集合保持不变,枚举器仍然有效。如果对集合进行了更改(例如添加,修改或删除元素),则枚举数将无法恢复,并且其行为未定义。

It's easy enough to get around this, though. Simply make a copy of the Dictionary (or it's keys) and iterate through it, adding/removing/etc. from the original Dictionary as needed.

不过,这很容易解决这个问题。只需复制字典(或它的键)并迭代它,添加/删除/等。根据需要从原始字典。

#3


You can't modify a dictionary when enumerating, simple as that. Otherwise when do you know you're done?

枚举时不能修改字典,这很简单。否则什么时候知道你已经完成了?

#4


When enumerating a collection (i.e. a foreach loop), it is not possible to add or remove items from the collection. Think of all the issues that would bring if that were allowed.

枚举集合(即foreach循环)时,无法在集合中添加或删除项目。想想如果允许的话会带来的所有问题。

#5


That's just... odd. You're trying to add the same person again for each person already in the dictionary.

那只是......很奇怪。您正在尝试为字典中已有的每个人再次添加同一个人。

Also: why store the age as a string? int (or even DateTime) seems much more appropriate.

另外:为什么把年龄存成一个字符串? int(甚至DateTime)似乎更合适。

#6


Because it will change the items of the Dictionary that you are scrolling... It has no sense... (and you may looping forever)

因为它会改变你正在滚动的词典的项目...它没有意义......(你可以永远循环)

#7


You are attempting to alter the number of entries in the collection while iterating over the loop. A for loop can accomplish this, but the foreach iterater will not allow you do so.

您正在尝试在迭代循环时更改集合中的条目数。 for循环可以实现这一点,但是foreach迭代器不允许你这样做。

#8


Dictionary<string, string> persons = new Dictionary<string, string>();
persons.Add("roger", "12");
persons.Add("mary", "13");
for (int i = 0; i < persons.Count; i++){ 
Console.WriteLine("Name: " + persons[i].Key + ", Age: " + persons[i].Value); 
persons.Add("Xico", "22");
}

Be aware that this is an infinite loop, because you are always adding a person before you get to the last entry.

请注意,这是一个无限循环,因为您总是在到达最后一个条目之前添加一个人。

#1


If you want to do this you should get a copy of the Dictionary Object (can be done in many ways) then enumerate it, add what you want to the original.

如果你想这样做,你应该得到一个字典对象的副本(可以通过多种方式完成),然后枚举它,添加你想要的原始对象。

Simply you can't add new items while enumerating an Dictionary.

在枚举字典时,您无法添加新项目。

#2


It's because of the way the enumerator is constructed.

这是因为构造枚举器的方式。

From MSDN Dictionary.Enumerator:

来自MSDN Dictionary.Enumerator:

An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying, or deleting elements, the enumerator is irrecoverably invalidated and its behavior is undefined.

只要集合保持不变,枚举器仍然有效。如果对集合进行了更改(例如添加,修改或删除元素),则枚举数将无法恢复,并且其行为未定义。

It's easy enough to get around this, though. Simply make a copy of the Dictionary (or it's keys) and iterate through it, adding/removing/etc. from the original Dictionary as needed.

不过,这很容易解决这个问题。只需复制字典(或它的键)并迭代它,添加/删除/等。根据需要从原始字典。

#3


You can't modify a dictionary when enumerating, simple as that. Otherwise when do you know you're done?

枚举时不能修改字典,这很简单。否则什么时候知道你已经完成了?

#4


When enumerating a collection (i.e. a foreach loop), it is not possible to add or remove items from the collection. Think of all the issues that would bring if that were allowed.

枚举集合(即foreach循环)时,无法在集合中添加或删除项目。想想如果允许的话会带来的所有问题。

#5


That's just... odd. You're trying to add the same person again for each person already in the dictionary.

那只是......很奇怪。您正在尝试为字典中已有的每个人再次添加同一个人。

Also: why store the age as a string? int (or even DateTime) seems much more appropriate.

另外:为什么把年龄存成一个字符串? int(甚至DateTime)似乎更合适。

#6


Because it will change the items of the Dictionary that you are scrolling... It has no sense... (and you may looping forever)

因为它会改变你正在滚动的词典的项目...它没有意义......(你可以永远循环)

#7


You are attempting to alter the number of entries in the collection while iterating over the loop. A for loop can accomplish this, but the foreach iterater will not allow you do so.

您正在尝试在迭代循环时更改集合中的条目数。 for循环可以实现这一点,但是foreach迭代器不允许你这样做。

#8


Dictionary<string, string> persons = new Dictionary<string, string>();
persons.Add("roger", "12");
persons.Add("mary", "13");
for (int i = 0; i < persons.Count; i++){ 
Console.WriteLine("Name: " + persons[i].Key + ", Age: " + persons[i].Value); 
persons.Add("Xico", "22");
}

Be aware that this is an infinite loop, because you are always adding a person before you get to the last entry.

请注意,这是一个无限循环,因为您总是在到达最后一个条目之前添加一个人。