你怎麼能最好地解釋爲什麼如果你複製它,使用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