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.
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.
When I read the code inside .NET, more specifically
ListBox.cs
andListView.cs
, they have two different classes for keeping theirSelectedItems
collections.ListBox.cs
hasSelectedObjectCollection
, which has these members:ListView.cs
hasSelectedListViewItemCollection
, which has these members only: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
'sSelectecObjectCollection
's GetEnumerator method looks like this:And
ListView.cs
'sSelectedListViewItemCollection
's GetEnumerator method looks like this:So it looks like
ListView
returns an enumerator of an array, which is constant, whilstListBox
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.