An example for using predicate to replace 'if' in c#?

c#, predicate

Solution

No matter what they say, if is not evil. There may be specific cases for which a Predicate is a better choice than an if (or a set of ifs).

For example,

 foreach (Foo f in fooList) {
     if (f.Equals(fooTarget)) {
        return f;
     }
 }

versus (.NET 2.0)

 fooList.Find(delegate (Foo f) { return f.Equals(fooTarget); });

or (later)

 fooList.Find(f => f.Equals(fooTarget));

Problem

I read that the 'if' keyword is evil, and better to use predicate to replace if. Then I googled, but still dont get it. Can anyone be kind to provide an example?

Original source

Related problems