5 ms·
Well, what happens when you try to remove the 10th page when there's only 1 left? As a rule of thumb, don't ever rely on indexation in a collection if you do r
by IvoDankolov 15y ago
Well, what happens when you try to remove the 10th page when there's only 1 left?
As a rule of thumb, don't ever rely on indexation in a collection if you do random deletes. Usually you'll just blow up your app gracelessly. Sometimes, epic failure ensues.
Through some feats of logic we might deduce that there's always a first element, though, until the collection is empty. So you might do this inside the loop:
MyControl.TabPages.RemoveAt(0)
Needless to say, calling Remove when you have the bloody index (on IList collections that is) is counter-productive.
And I guess that code would be okay. I mean, if you head to phrase it : let x be the number of elements in the list, take out the head of the list that many times.
However, is it really the fastest way to clear a list? No. If we could access the class internals, we could just replace the store with a new empty array. Voila, O(1) clear and the garbage collector takes out the trash for you.
Generally, though, don't spend time worrying about implementation if you already have one available. When you've done optimizing all of your stuff (which is never the case), then you could go on to suggest changes to the standard library.
- deleted 15y ago[deleted]