For removing an object where a property equals a value which is faster?
foreach(object o in objects)
{
if(o.name == "John Smith")
{
objects.Remove(o);
break;
}
}
or
objects.RemoveAll(o => o.Name == "John Smith");
Thanks!
EDIT:
I should have mentioned this is removing one object from the collection, then breaking out of the loop which prevents any errors you have described, although using a for loop with the count is the better option!
From a
List<string>of 10,000 items, the speeds are:From this information, we can conclude that the lambda expression is faster.
The source code I used can be found here.
Note that I substituted your
foreachwith aforloop, since we aren’t able to modify values within aforeachloop.