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]);
    }
 }

No comments:

Post a Comment

Your comments, Feedbacks and Suggestions are very much valuable to me :)

Things are upgraded

My Dear readers, I am really thankful for being supportive all these years. This site was the first blog site I ever created in my life...