Home » CodeDigest


Predicate Delegate with Example in C#
Submitted By Satheesh Babu B
On 11/26/2008 7:34:09 AM
Tags: C#,CodeDigest  

Predicate Delegate with Example in C#


Predicate Delegate is a new feature in C# 2.0. If we want to search for an item in an Array or in a List, we prefer using foreach statement for to iterate and search the item. The use foreach statement will make the code messy and confusing with some performance issue. We can use the Predicate delegate in these scenarios. As a definition, predicate delegate is a method that defines set of criteria and determines whether a object passes the criteria.

The syntax for Predicate method should be like,


public delegate bool Predicate (

T obj



The method should return true, if the criteria is satisfied else it should be false.



I am having array of names and to search for a name that starts with "S",the predicate method will be,


public static bool Search(string str) {

return str.StartsWith("S", StringComparison.InvariantCultureIgnoreCase);



To use this Predicate method for Searching,


Array.Find(str, Search);


here "str" is the array that holds the names.


As i said earlier, predicates can be used with Array and List.


For Array

Array.Find(str, Search);


For List



Here,"emplist" is the Generic list that holds list of Employee entity.

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..