Coder Perfect

C# Predicate Delegates

Problem

Could you please explain to me:

It will be very appreciated if you can provide detailed source code.

Asked by Canavar

Solution #1

A function that returns true or false is referred to as a predicate. A reference to a predicate is referred to as a predicate delegate.

A predicate delegate is essentially a pointer to a function that returns true or false. Here’s an example of how predicates can be used to filter a list of values.

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<int> list = new List<int> { 1, 2, 3 };

        Predicate<int> predicate = new Predicate<int>(greaterThanTwo);

        List<int> newList = list.FindAll(predicate);
    }

    static bool greaterThanTwo(int arg)
    {
        return arg > 2;
    }
}

If you’re using C# 3, you can now use a lambda to represent the predicate in a more readable way:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<int> list = new List<int> { 1, 2, 3 };

        List<int> newList = list.FindAll(i => i > 2);
    }
}

Answered by Andrew Hare

Solution #2

Following up on Andrew’s response on c#2 and c#3, you can also do them inline for a one-time search function (see below).

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<int> list = new List<int> { 1, 2, 3 };

        List<int> newList = list.FindAll(delegate(int arg)
                           {
                               return arg> 2;
                           });
    }
}

Hope this helps.

Answered by WestDiscGolf

Solution #3

It’s just a boolean-returning delegate. It’s commonly used in list filtering, but it can be applied everywhere.

List<DateRangeClass>  myList = new List<DateRangeClass<GetSomeDateRangeArrayToPopulate);
myList.FindAll(x => (x.StartTime <= minDateToReturn && x.EndTime >= maxDateToReturn):

Answered by Adam Carr

Solution #4

Although it’s from the.NET2 era, there’s no mention of lambda expressions, there’s an excellent article on predicates here.

Answered by LukeH

Solution #5

What is the meaning of Predicate Delegate?

1) A property that returns true or false is known as a predicate. The.net 2.0 framework introduced this idea. 2) It’s used in conjunction with a lambda expression (=>). As an argument, it accepts generic type. 3) It allows you to define a predicate function and provide it as a parameter to another function. 4) It’s a specific case of a Func in that it only accepts one parameter and returns a bool.

In C# namespace:

namespace System
{   
    public delegate bool Predicate<in T>(T obj);
}

It’s in the System namespace where it’s defined.

When should Predicate Delegate be used?

In the following situations, we should use Predicate Delegate:

1) To find objects in a broad collection. e.g.

var employeeDetails = employees.Where(o=>o.employeeId == 1237).FirstOrDefault();

2) The following is a simple example that shortens the code and returns true or false:

Predicate<int> isValueOne = x => x == 1;

Now, call the predicate mentioned above:

Console.WriteLine(isValueOne.Invoke(1)); // -- returns true.

3) As shown below, an anonymous method can be given to a Predicate delegate type:

Predicate<string> isUpper = delegate(string s) { return s.Equals(s.ToUpper());};
    bool result = isUpper("Hello Chap!!");

Do you have any suggestions for predicates best practices?

Instead of Predicates, use Func, Lambda Expressions, and Delegates.

Answered by Gul Ershad

Post is based on https://stackoverflow.com/questions/556425/predicate-delegates-in-c-sharp