19 ms·
Isn't clear() itself a "while (not empty) delete element"?
by altrego99 15y ago
Isn't clear() itself a "while (not empty) delete element"?
- dkersten 15y agoMaybe, maybe not - depends on the container. But thats not the point - while (not empty) is not iterating over the loop, so there is no danger of corrupting the iterator.
- benhoyt 15y agoVery unlikely. There class implementation can almost certainly do it with an O-1 operation, like "self._internal_count = 0".
- jcromartie 15y agoor self._tabs = new List();
- eropple 15y agoIt might be. It also might be a "new List<TabPage>" or even some faster, internal construct. I don't think .NET makes a promise here, though I could be wrong.
- StrawberryFrog 15y agoA while loop isn't in itself an iterator. There's a difference between while(collection.Count > 0) { collection.Delete(0); } .. which is a working implementation of clear, though probably superfluous. and... int i = 0; while(i < collection.Count) { collection.Delete(i); i++; } which does try to walk along the list and is broken. Also, .Net has a specific exception to stop you modifying a collection while an enumerator is walking along it. Google for "Collection was modified; enumeration operation may not execute"