I want to do something like this:
List list1 = ...
List list2 = ...
Predicate condition = ...
...
list2.
You could use a lambda expression to define an anonymous delegate inplace that is the result of negating the result of the predicate:
list.RemoveAll(x => !condition(x));
Another option:
static Predicate Negate(Predicate predicate) {
return x => !predicate(x);
}
Usage:
// list is List some T
// predicate is Predicate some T
list.RemoveAll(Negate(predicate));
The reason that list.RemoveAll(!condition) does not work is that there is no ! operator defined on delegates. This is why you must define a new delegate in terms of condition as shown above.