Introduction to C# Predicate
An in-built generic type delegate is a predicate delegate in C# which is defined under the Namespace System. Namespace and the methods containing certain set of criteria can be worked with predicate delegate to determine if the parameter that is passed can fulfill the given criteria or not and only one input is taken by this criteria returning the values either true or false and the predicate delegate is same as the other delegates Func delegate and Action delegate.
Syntax:
public delegate bool Predicate <in P>(P obj);
Where the object type is represented by P and obj is the object which compares the criteria that is defined within a method and is represented by predicate delegate.
Working of Predicate Delegate in C#
- A function that returns true or false is a predicate and the reference to a predicate is a predicate delegate.
- The feature of predicate delegate was introduced with the release of .NET 2.0. framework.
- The predicate function can be defined, and it can be passed as a parameter to any other function through predicate delegate.
- A special case of Func is predicate delegate which takes only one parameter as input and it returns a Boolean value that is either true of false.
- Any method can be written inside a predicate delegate even the expression of lambda or anonymous method.
- A generic type is taken as an argument by the predicate delegate when it is used with the expression of lambda.
Examples of C# Predicate
Given below are the examples mentioned:
Example #1
C# program to demonstrate the use of predicate delegate in a program to check if the given string passed as a parameter is in capital letters or not.
Code:
using System;
//a namespace called program is defined
namespace program
{
//a class called check is defined
public class check
{
//a Boolean method is defined to check if the given string is written in capital letters or not. If written in capital letters, true is returned else False is returned.
static bool IsUC(string stri)
{
return stri.Equals(stri.ToUpper());
}
//main method is called
static void Main(string[] args)
{
//a predicate delegate is defined with object type as string and IsUC is an object which compares the criteria that is defined within a method and is represented by predicate delegate.
Predicate<string> isU = IsUC;
//The result of the predicate delegate is stored in a variable called res
bool res = isU("welcome to c#");
//the result is displayed
Console.WriteLine(res);
}
}
}
Output:
Explanation:
- In the above program, a namespace called program is defined. Then a class called check is defined. Then a Boolean method is defined to check if the given string is written in capital letters or not. If the given string is written in capital letters, true is returned else False is returned. Then the main method is called.
- Then a predicate delegate is defined with object type as string and IsUC is an object which compares the criteria that is defined within a method and is represented by predicate delegate. Then the result of the predicate delegate is stored in a variable called res. Then the result is displayed.
Example #2
C# program to demonstrate the use of predicate delegate in a program to check if the length of the given string is less than a specified value or not.
Code:
using System;
//a class called program is defined
class program
{
// a predicate delegate is defined with object type as string
public delegate bool my_del(string stri);
// a method is defined inside a predicate delegate by passing the object as parameter to check if the length of the given string is less than a specified value. If less than the given specified value, true is returned else false is returned
public static bool fun(string stri)
{
if (stri.Length < 5)
{
return true;
}
else
{
return false;
}
}
//Main method is called
static public void Main()
{
// a predicate delegate is defined with object type as string and fun is an object which compares the criteria that is defined within a method and is represented by predicate delegate.
my_del obj = fun;
//The string to be passed as a parameter to predicate delegate is written here
Console.WriteLine(obj("Shobha"));
}
}
Output:
Explanation:
- In the above program, a class called program is defined. Then a predicate delegate is defined with object type as string. Then a method is defined inside a predicate delegate by passing the object as parameter to check if the length of the given string is less than a specified value. If the length of the string is less than the given specified value, true is returned else false is returned.
- Then the Main method is called. Then a predicate delegate is defined with object type as string and fun is an object which compares the criteria that is defined within a method and is represented by predicate delegate. Then the string to be passed as a parameter to predicate delegate is written at last.
Advantages
Given below are the advantages of C# Predicate:
- The Predicate delegates are useful when we have to filter out a list of values.
- The predicate delegates can be made inline for one off search function.
- The predicate delegates can be used when we have to search for items in a generic collection.
- By using predicate delegates, the length of the code is shortened and either true or false is returned.
- Anonymous methods, expression of lambda can be assigned to predicate delegates.
- The predicate delegates provide the logic at runtime and it can be a simple logic or complicated logic.
Recommended Articles
This is a guide to C# Predicate. Here we discuss the working of predicate delegate in C#, advantages and programming examples respectively. You may also have a look at the following articles to learn more –