Table of contents
1.
Introduction
2.
Syntax
3.
Predicate Delegate With An Anonymous Methord
4.
Predicate Delegate With The Lambda Expressions
5.
Frequently Asked Questions
5.1.
How many input parameters can a predicate delegate accept?
5.2.
In which namespace Predicate delegate is specified?
5.3.
How do you use a predicate delegate?
5.4.
Can predicate delegate be used with an anonymous function?
5.5.
Can a predicate delegate be used with a lambda expression?
6.
Conclusion
Last Updated: Mar 27, 2024

C# Predicate Delegate

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A Predicate delegate is a generic type of delegate built into the language. It is a delegate, similar to Func and Action delegates. It represents a method with a set of criteria that checks to see if the passed parameter meets those criteria. A predicate delegate method must accept one input parameter and return a boolean value, either true or false.

Syntax

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


T is the type of the object, and obj is the object that will be compared against the criteria provided within the method represented by the Predicate delegate. The Predicate delegate is specified in the System namespace. It has one input argument and a boolean return type. Predicate, like the other delegate types, can be used with any method, anonymous method, or lambda expression.

Example 

class PredicateExample {
  
    public static bool fun(string mystring)
    {
        if (mystring.Length < 7)
        {
            return true;
        }
        else 
        {
            return false;
        }
    }
  
    static public void Main()
    {
        Predicate<string> val = fun;
        Console.WriteLine(val("This is an Example"));
    }
}


OutputFalse

Predicate Delegate With An Anonymous Methord

As seen in the following example, you can also use a Predicate delegate with an anonymous method:
Example:

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

 

Must Read IEnumerable vs IQueryable.

Predicate Delegate With The Lambda Expressions

As seen in the following example, you can also utilise a Predicate delegate with lambda expressions:

Predicate<string> val = str = > str.Equals(str.ToLower());
val("This is an Example");

 

You can also check out Multithreading in Python Multithreading in C#, singleton design pattern in c#

Frequently Asked Questions

How many input parameters can a predicate delegate accept?

Only one input parameter can predicate delegate accept. 

In which namespace Predicate delegate is specified?

The Predicate delegate is defined in the System namespace same as other delegates.

How do you use a predicate delegate?

A predicate delegate method must accept one input parameter and return a boolean value, either true or false. Predicate syntax - public delegate bool Predicate<in T>(T obj);

Can predicate delegate be used with an anonymous function?

Yes. As with other delegate types, a predicate can also be used with any method, anonymous method, or lambda expression.

Can a predicate delegate be used with a lambda expression?

Yes. As with other delegate types, a predicate can also be used with any method, anonymous method, or lambda expression.

Conclusion

In this article, we had a look at Predicate Delegate in C#. We hope that this blog has helped you enhance your knowledge regarding Predic Delegate and if you would like to learn more,  check out Delegates in C#  and Methods in C# also other articles on Coding Ninjas Studio. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass