for (int i=this.MyControl.TabPages.Count - 1; i > 0; i--) { this.MyControl.TabPages.Remove(this.MyControl.TabPages[i]); }
Imagine the count is 2. The first iteration you delete item 1, the second you delete item 0, and then the loop exits.
EDIT: Actually, as someone else pointed out, it's clearer to use a while loop that deletes the 0th item until the collection is empty.
I think what you meant was:
for (int i=this.MyControl.TabPages.Count; i > 0; i--) { this.MyControl.TabPages.Remove(this.MyControl.TabPages[i-1]); }
for (int i=this.MyControl.TabPages.Count - 1; i >= 0; i--) {
this.MyControl.TabPages.Remove(this.MyControl.TabPages[i]);
}
Though a simple while loop is much easier to follow, even if its less efficient than removing the elements in reverse.while (MyControl.TabPages.Count > 0) { MyControl.TabPages.RemoveAt(MyControl.TabPages.Count-1); }
For loop are nothing more than while loops with:
(1) an assignment (int i = MyControl.TabPages.Count in this case)
(2) an extra command (i-- in this case) added to the end
Regarding for vs while, I find the choice is important only in the intent they emphasize: while puts emphasis on the condition, whereas for puts the emphasis on the iteration. I think in this case the condition (that the list is not empty) is deserves more emphasis than the iteration through the elements of said list - hence why I find the while version to be more readable. YMMV and all that :)
Ten years ago, sure, but nowadays I trust the compiler to do this for me ;-)
It's TFA's method, except broken (or not fixed, word it as you prefer).