Very often I need something like that:
foreach (Line line in lines)
{
if (line.FullfilsCertainConditions())
{
lines.Remove(line)
}
}
This is baked directly into List
:
lines.RemoveAll(line => line.FullfilsCertainConditions());
or in C# 2.0:
lines.RemoveAll(delegate(Line line) {
return line.FullfilsCertainConditions();
});
In the non-List
case (your edit to the question), you could wrap this something like below (untested):
static class CollectionUtils
{
public static void RemoveAll(IList list, Predicate predicate)
{
int count = list.Count;
while (count-- > 0)
{
if (predicate(list[count])) list.RemoveAt(count);
}
}
public static void RemoveAll(IList list, Predicate
Since UIElementCollection
implements the (non-generic) IList
this should work. And quite conveniently, with C# 3.0 you can add a this
before IList
/ IList
and have it as an extension method. The only subtlety is that the parameter to the anon-method will be object
, so you'll need to cast it away.