Predicate Delegates in .NET Core C#: Syntax, Usage and Examples

We already learned about the below delegates.

If you haven’t looked, first please learn about those delegates.

Introduction

Like Func and Action delegates, the predicate is a delegate. It symbolizes a procedure that verifies that the passed parameter satisfies a set of requirements. One input parameter must be provided by a predicate delegate method, which must then return a boolean (true or false).

As can be seen below, the Predicate delegate is defined in the System namespace.

Syntax

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

In this case, obj is the object that will be compared to the criteria specified in the method that the predicate delegate is representing, and T is the type of the object.

The predicate can be used with any method, anonymous method, or lambda expression, just like other delegate types.

public delegate bool CheckLength(string text);
static bool IsAny(string text)
{
    if (text.Length < 7)
    {
        return true;
    }
    else
    {
        return false;
    }
}
CheckLength checkLength = IsAny;
Console.WriteLine("Validate: {0}", checkLength("Jaimin"));
Console.WriteLine("\nValidate: {0}", checkLength(""));
Console.WriteLine("\nValidate: {0}", checkLength("Jaimin Shethiya"));

Anonymous method

As seen below, we are now using the same program mentioned above with a predicate delegate.

Rather than using a custom delegate, we use a predicate delegate in the example below. It makes the program easier to read and minimizes the amount of code. The Predicate delegate in this case has a single input parameter and a boolean return value. Here, we immediately designate the Predicate delegate with an IsAny method.

static bool IsAny(string text)
{
    if (text.Length < 7)
    {
        return true;
    }
    else
    {
        return false;
    }
}

Predicate<string> checkLength = IsAny;
Console.WriteLine("Validate: {0}", checkLength("Jaimin"));

Console.WriteLine("\nValidate: {0}", checkLength(""));

Console.WriteLine("\nValidate: {0}", checkLength("Jaimin Shethiya"));

Boolean return value

As demonstrated below, a Predicate delegate type can also be assigned an anonymous method.

Predicate<string> checkLength = delegate (string text)
{
    if (text.Length < 7)
    {
        return true;
    }
    else
    {
        return false;
    }
};

Console.WriteLine("Validate: {0}", checkLength("Jaimin"));

Console.WriteLine("\nValidate: {0}", checkLength("Jaimin Shethiya"));

Predicate delegate

As demonstrated below, a Predicate delegate type can also be assigned a lambda expression.

Predicate<string> checkLength = (text) =>
{
    if (text.Length < 7)
    {
        return true;
    }
    else
    {
        return false;
    }
};

Console.WriteLine("Validate: {0}", checkLength("Jaimin"));

Console.WriteLine("\nValidate: {0}", checkLength("Jaimin Shethiya"));

Lambda expression

Crucial points to remember

  • The predicate delegate requires a boolean return type and a single input parameter.
  • The predicate delegate can have an anonymous method and a lambda expression assigned to it.

We learned the new technique and evolved together.

Happy coding!


Similar Articles