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

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -