Predicate Delegate in C#

One built-in generic type delegate is the Predicate delegate in C#. The predicate delegate is defined under the System namespace. It works with those methods that have a set of criteria and determines if the parameter that has been supplied fulfils or fails to meet the requirements. With a single input, this delegate returns a result that can be either true or false.

Syntax:

It has the following syntax:

  • public: This keyword indicates that the delegate can be accessed from any outside class or assembly in which it is included. It also indicates that any code that has access to the contained namespace or assembly can use the delegate.
  • delegate: This keyword indicates that a delegate type is being declared. A type that represents references to methods with a certain parameter list and return type is called a delegate.
  • bool: It describes the delegate's return type. In this case, it is bool, meaning that the delegate will provide a boolean value (true or false).
  • Predicate: The declaration of the delegate type is indicated by this name. The Predicate delegate type in .NET is frequently used to describe a method that checks to see whether an object meets a certain condition.
  • <in P>: The generic type parameter P for the delegate is specified in this part. The keyword before P denotes the contravariant nature of the P-type parameter. It indicates that you can pass a type that is derived from P, or even P itself, as an argument to the delegate in this case.
  • (P obj): The parameter list for the delegate is represented by (P obj). The delegate is only required to accept a single type P parameter called obj. A method that takes an argument of type P would be provided when using this delegate.

Common Use Cases

Several use cases of the Predicate Delegate in C# are as follows:

  1. Filtering: Predicates are frequently used with methods like Find, FindAll, Exists, etc. to filter elements based on certain criteria.
  2. Validation: Before processing data, predicates can be used to check it against certain requirements.
  3. Searching: They are useful for searching through collections or arrays to find elements that match a particular condition.
  4. Predicates can be passed as parameters to methods that take delegates, such as List methods, LINQ extension methods (Where, Any, All, etc.), and more.
  5. They can also be assigned to delegate variables and used for method group conversions.

Example 1:

Let us take an example to illustrate the Predicate Delegates in C#.

Output:

The Even Elements are:
2 4 6 8 10 12 
The Odd Elements are:
1 3 5 7 9 11 
The Multiples of Four are:
4 8 12

Explanation:

Overall, this code demonstrates the versatility of Predicate delegates in C#. It supports named methods, anonymous methods, and lambda expressions to set filtering criteria. The predicate delegate enhances code reusability and readability, making it easier to filter elements from a collection using custom criteria.

Example 2:

Let us take another example to illustrate the Predicate Delegates in C#.

Output:

True

Conclusion:

In conclusion, the program shows how to determine whether a given string is in uppercase by using delegates and method group conversion. This example demonstrates how methods with corresponding signatures may be represented by delegates, enabling flexible and adaptable method invocation.






Latest Courses