17 October 2010

Removing Items from a Collection

----> Its seems easier than it is.... Just loop through an array of items and remove items from it.However,when you
remove an item from a collection while looping through it.the size of the collection changes and the loop may fail(depending on how you are looping).
The correct way to loop through a collection is to essentially loop through it backwardsmstarting with the last and moving towards 0'th element.

  For(int i=MyList.count-1;i>=0;i--)
 {
     if(MyList.Items[i].Value!=SomeValue)
    {
        MyList.Remove(MyList.Items[i]);
    }
 }