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]);
}
}
MyList.Remove(MyList.Items[i]);
}
}
No comments:
Post a Comment
Your comments, Feedbacks and Suggestions are very much valuable to me :)