Why foreach works while removing items from ListView and doesn't work from ListBox?

▼魔方 西西 提交于 2020-01-03 19:37:16

问题


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.


回答1:


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.




回答2:


while (myListBox.SelectedItems.Count > 0)
{
    myListBox.Items.Remove(myListBox.SelectedItems[0]);
}



回答3:


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.



来源:https://stackoverflow.com/questions/8379072/why-foreach-works-while-removing-items-from-listview-and-doesnt-work-from-listb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!