我在 Silverlight 工具包中看到过类似以下的代码,但无法理解如何安全地执行此操作:
private void ItemsSourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
// Update the cache
if (e.Action == NotifyCollectionChangedAction.Remove && e.OldItems != null)
{
for (int index = 0; index < e.OldItems.Count; index++)
{
_items.RemoveAt(e.OldStartingIndex);
}
}
}
如果您从说索引 5 中删除一个项目,这不会将_items
索引 5 之后集合中每个项目的当前索引更改为比以前少一个吗?那么,像这段代码那样使用它们的“旧”索引持续删除项目是安全的吗?
我真的很想了解为什么会这样。
有任何想法吗?