c# - How to invert a System.Predicate<T> -
let's have predicate<t>
, , want call method foo said predicate returning true, , want call method bar predicate returning false, how can this:
predicate<int> p = => > 0 foo(p); bar(!p); // doesn't work
if predicate func<int, bool> f
, !f(i)
you can create method return "inverted" predicate - make extension method:
public static predicate<t> invert<t>(this predicate<t> predicate) { // todo: nullity checking return x => !predicate(x); }
then:
bar(p.invert());
basically it's you'd if had func<int, bool>
- there's nothing "magic" either of delegate types.
Comments
Post a Comment