C# | Predicate Delegate

A Predicate delegate is an in-built generic type delegate. This delegate is defined under System namespace. It works with those methods which contain some set of criteria and determine whether the passed parameter fulfill the given criteria or not. This delegate takes only one input and returns the value in the form of true or false. Now, first of all, we see how custom delegates work in this situation. As shown in the below example.

Syntax:

public delegate bool Predicate <in P>(P obj);

Here, P is the type of the object and obj is the object which is going to compare against the criteria defined within the method represented by Predicate delegate.



Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate delegates
using System;
  
class GFG {
  
    // Declaring the delegate
    public delegate bool my_delegate(string mystring);
  
    // Method
    public static bool myfun(string mystring)
    {
        if (mystring.Length < 7) 
        {
            return true;
        }
  
        else
        {
            return false;
        }
    }
  
    // Main method
    static public void Main()
    {
  
        // Creating object of my_delegate
        my_delegate obj = myfun;
        Console.WriteLine(obj("Hello"));
    }
}

chevron_right


Output:

True

Now, we use the same above program with Predicate delegate as shown below.

Example: In the below example, we use a predicate delegate instead of a custom delegate. It reduces the size of the code and makes the program more readable. Here, the Predicate delegate contains a single input parameter and return output in boolean type. And here, we directly assign a myfun method to the Predicate delegate.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate Predicate delegates
using System;
  
class GFG {
  
    // Method
    public static bool myfun(string mystring)
    {
        if (mystring.Length < 7)
        {
            return true;
        }
        else 
        {
            return false;
        }
    }
  
    // Main method
    static public void Main()
    {
  
        // Using predicate delegate
        // here, this delegate takes
        // only one parameter
        Predicate<string> val = myfun;
        Console.WriteLine(val("GeeksforGeeks"));
    }
}

chevron_right


Output:

False

Important Points:

  • You can also use a Predicate delegate with an anonymous method as shown in the below example:

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    Predicate<string> val = delegate(string str)
    {
        if (mystring.Length < 7)
        {
            return true;
        }
        else 
        {
            return false;
        };
        val("Geeks");

    chevron_right

    
    

  • You can also use a Predicate delegate with the lambda expressions as shown in the below example:

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    Predicate<string> val = str = > str.Equals(str.ToLower());
    val("Geeks");

    chevron_right

    
    



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.