你怎么能最好地解释为什么如果你复制它,使用foreach替换你正在循环的集合的元素是被允许的。例如:为什么你不能修改每个循环中的集合
foreach(Item item in Items)
{
item.modify //or remove or add
}
// will not work
foreach(Item item in Items.ToList())
{
item.modify //or remove. or add
}
//will work altough i dont get it because i am now iterating trough the temporary list
//and changing its elements.
//In my understanding its not like im iterating the list(.ToList) and modifying the source items
//(Items). A graphic representation would be welcome, my interest is to understand the
//matter logically
因为如果你**榜上无名的副本**然后除去项目*原始列表*(它*不*被重复!)不会影响副本。 – 2012-05-09 06:48:07
您需要找到列表的内部以及项目如何连接在一起,以及迭代器如何从一个元素移动到另一个元素,以了解为什么会发生这种情况(副本旁边)。这些列表通过指针连接在一起。从列表中删除项目时,所有列表都会更改,并且下一个循环中的迭代器不能相同。 – Aristos
如果它使用列表进行迭代,那么logicaly它将使用相同的列表从中删除。因为即时通讯使用列表对象进行此操作。 – Freeman