As most of you know, changing collection while iterating is strictly forbidden. Or, actually – not forbidden, but gets us into serious problems, which ends up with an exception. As an example:
Now, when trying to get third element of the collection (not removing the second one!!!) we receive beautiful message "Collection was modified; enumeration operation may not execute."
There's a small trick we may use, but please – don't laugh too loud!:)
This time we have no exception and the collection is being modified. What about the cost! But who cares! ;)
And finally, for some enumerables we may use the 'inline' shortcut, the 'tricky' one:
No additional classes, very simple usage of the List(IEnumerable ie) ctor. Nice, isn't it?
11-26-2006 7:28 PM