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<T> Negate<T>(Predicate<T> predicate) {
return x => !predicate(x);
}
Usage:
// list is List<T> some T
// predicate is Predicate<T> 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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…