For training related to .net,OOPS,design patterns, MVC,WCF and .net fundamentals
contact at sukeshmarla@gmail.com or Click Here

Click here for my training feedbacks

Followers

Follow Us



Share your Interview questions here


For software development queries contact SukeshMarla@Gmail.com

Follow by Email

Learn everything about JSON in a day

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