Home > Article > Backend Development > 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.
Given below are the examples mentioned:
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:
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:
Given below are the advantages of C# Predicate:
The above is the detailed content of C# Predicate. For more information, please follow other related articles on the PHP Chinese website!