tags:

views:

610

answers:

4

I am very new to predicate.

just learned how to write a predicate like

Predicate<int> pre = delegate(int a){ a %2 == 0 };

what will the predicate return ?

and how is it useful when programing ?

+5  A: 

The Predicate will always return a boolean, by definition.

Predicate<T> is basically identical to Func<T,bool>.

Predicates are very useful in programming. They are often used to allow you to provide logic at runtime, that can be as simple or as complicated as necessary.

For example, WPF uses a Predicate<T> as input for Filtering of a ListView's ICollectionView. This lets you write logic that can return a boolean determining whether a specific element should be included in the final view. The logic can be very simple (just return a boolean on the object) or very complex, all up to you.

Reed Copsey
Just a pity they are not :)
leppie
Delegates are useful in programming. Quite frankly I find the name Predicate very unhelpful as this question clearly proves. If you really want to describe what a predicate does you would name it Filter<T>.
ChaosPandion
Not what? [15 chars]
Ed Swangren
@Ed: I think he was suggesting that it's a pity they aren't just Func<T,bool>
Reed Copsey
They are not the same type and hence not convertible between each other.
leppie
A: 

In C# Predicates are simply delegates that return booleans. They're useful (in my experience) when you're searching through a collection of objects and want something specific.

I've recently run into them in using 3rd party web controls (like treeviews) so when I need to find a node within a tree, I use the .Find() method and pass a predicate that will return the specific node I'm looking for. In your example, if 'a' mod 2 is 0, the delegate will return true. Granted, when I'm looking for a node in a treeview, I compare it's name, text and value properties for a match. When the delegate finds a match, it returns the specific node I was looking for.

osij2is
+27  A: 

Predicate<T> is a functional construct providing a convenient way of basically testing if something is true of a given T object.

For example suppose I have a class:

class Person {
    public string Name { get; set; }
    public int Age { get; set; }
}

Now let's say I have a List<Person> people and I want to know if there's anyone named Oscar in the list.

Without using a Predicate<Person> (or Linq, or any of that fancy stuff), I could always accomplish this by doing the following:

Person oscar = null;
foreach (Person person in people) {
    if (person.Name == "Oscar") {
        oscar = person;
        break;
    }
}

if (oscar != null) {
    // Oscar exists!
}

This is fine, but then let's say I want to check if there's a person named "Ruth"? Or a person whose age is 17?

Using a Predicate<Person>, I can find these things using a LOT less code:

Predicate<Person> oscarFinder = (Person p) => { return p.Name == "Oscar"; };
Predicate<Person> ruthFinder = (Person p) => { return p.Name == "Ruth"; };
Predicate<Person> seventeenYearOldFinder = (Person p) => { return p.Age == 17; };

Person oscar = people.Find(oscarFinder);
Person ruth = people.Find(ruthFinder);
Person seventeenYearOld = people.Find(seventeenYearOldFinder);

Notice I said a lot less code, not a lot faster. A common misconception developers have is that if something takes one line, it must perform better than something that takes ten lines. But behind the scenes, the Find method, which takes a Predicate<T>, is just enumerating after all. The same is true for a lot of Linq's functionality.

So let's take a look at the specific code in your question:

Predicate<int> pre = delegate(int a){ return a % 2 == 0; };

Here we have a Predicate<int> pre that takes an int a and returns a % 2 == 0. This is essentially testing for an even number. What that means is:

pre(1) == false;
pre(2) == true;

And so on. This also means, if you have a List<int> ints and you want to find the first even number, you can just do this:

int firstEven = ints.Find(pre);

Of course, as with any other type that you can use in code, it's a good idea to give your variables descriptive names; so I would advise changing the above pre to something like evenFinder or isEven -- something along those lines. Then the above code is a lot clearer:

int firstEven = ints.Find(evenFinder);
Dan Tao
+1 from me! By far the clearest answer on the page.
Philip Wallace
+1: Very well written. I would just like to add the one liner form MSDN: "Represents the method that defines a set of criteria and determines whether the specified object meets those criteria"
Partha Choudhury
amazing. faving the question too.
George
Thanks this is really a good post.Thank you
Jebli
Great answer +1
Phaedrus
+1  A: 

The Predicate will always return a boolean, by definition.

Predicate is basically identical to Func.

Predicates are very useful in programming. They are often used to allow you to provide logic at runtime, that can be as simple or as complicated as necessary.

For example, WPF uses a Predicate as input for Filtering of a ListView's ICollectionView. This lets you write logic that can return a boolean determining whether a specific element should be included in the final view. The logic can be very simple (just return a boolean on the object) or very complex, all up to you.

Example : -

   int[] intArray = { 1, 3, 4, 5, 67, 8, 899, 56, 12, 33 };

   Predicate<int> func = delegate(int val) { return val % 2 == 0; };

   Array.FindAll<int>(intArray, func);

the above code helps in finding all the even numbers from the integer array using predicate as a parameter to the FindAll function.

Thanks for all your answers.

Jebli