Why foreach works while removing items from ListVi

2019-06-24 03:44发布

I've started learning C# and I'm a bit confused about the behavior that I discovered. I try to figure out, why in one case the code is working and in another not:

foreach (ListViewItem l in listView1.SelectedItems) l.Remove();
foreach (object l in listBox1.SelectedItems) listBox1.Items.Remove(l);

First one works fine and there is no error, but the second one throws exception with information that the collection was changed.

Could anyone explain it to me?

PS. In case of ListView I was debugging code and collection SelectedItems was changing, but even though it worked well.

3条回答
姐就是有狂的资本
2楼-- · 2019-06-24 04:24

You cannot modify a collection over which you are enumerating. That's why you are getting an exception in the second example.

The Remove method on the ListView item is designed to not throw an exception in this situation.

查看更多
Emotional °昔
3楼-- · 2019-06-24 04:28
while (myListBox.SelectedItems.Count > 0)
{
    myListBox.Items.Remove(myListBox.SelectedItems[0]);
}
查看更多
forever°为你锁心
4楼-- · 2019-06-24 04:34

When I read the code inside .NET, more specifically ListBox.cs and ListView.cs, they have two different classes for keeping their SelectedItems collections.

ListBox.cs has SelectedObjectCollection, which has these members:

private ListBox owner;
private bool    stateDirty; 
private int     lastVersion; 
private int     count;

ListView.cs has SelectedListViewItemCollection, which has these members only:

private ListView owner;
private int lastAccessedIndex = -1;

So by looking at that, I guess I can deduce that ListBox's collection is a proper enumerator that keeps track of any changes and the number of items that are in the list. ListView, on the other hand, seems to not care about that at all, and only keep track of the current index of the enumerator and simply steps forward.

So ListBox throws the exception since it keeps track of modifications, ListView does not.

EDIT: ListBox.cs's SelectecObjectCollection's GetEnumerator method looks like this:

public IEnumerator GetEnumerator() {
    return InnerArray.GetEnumerator(SelectedObjectMask); 
}

And ListView.cs's SelectedListViewItemCollection's GetEnumerator method looks like this:

public IEnumerator GetEnumerator() { 
    if (owner.VirtualMode) { 
        throw new InvalidOperationException(SR.GetString(SR.ListViewCantAccessSelectedItemsCollectionWhenInVirtualMode));
    } 

    ListViewItem[] items = SelectedItemArray;
    if (items != null) {
        return items.GetEnumerator(); 
    }
    else { 
        return new ListViewItem[0].GetEnumerator(); 
    }
} 

So it looks like ListView returns an enumerator of an array, which is constant, whilst ListBox returns an actual enumerator as a filter of its InnerArray of items.

I know this is not what you asked about; but it is always favorable to add all items to a temporary List before looping through it to remove things, since you can never know how the enumerators are implemented on the backend, nor how they might change in the future.

查看更多
登录 后发表回答