I am pretty sure you all must have seen these delegates when writing code. IntelliSense shows methods that accept Actions, Func<TResult> and some accept Predicate<T>. So what are these? Let’s find out.
Let’s go by a simple example. I have following “Employee” class and it has a helper method which will return me a list of Employees.
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime Birthday { get; set; }
public int Age { get; set; }
public static List<Employee> GetEmployeees()
return new List<Employee>()
new Employee()
FirstName = "Jaliya",
LastName = "Udagedara",
Birthday = Convert.ToDateTime("1986-09-11")
},
FirstName = "Gary",
LastName = "Smith",
Birthday = Convert.ToDateTime("1988-03-20")
}
};
In my Main method I am getting the list of type employees into a variable
List<Employee> employees = Employee.GetEmployeees();
Action series of delegates are pointers to methods which take zero, one or more input parameters, and do not return anything.
So let’s create an Action now. I have the following method which will calculate the age of the employee when the employee is passed in.
static void CalculateAge(Employee emp)
emp.Age = DateTime.Now.Year - emp.Birthday.Year;
Action<Employee> empAction = new Action<Employee>(CalculateAge);
foreach (Employee e in employees)
Console.WriteLine(e.Age);
employees.ForEach(e => e.Age = DateTime.Now.Year - e.Birthday.Year);
Func<TResult> series of delegates are pointers to methods which take zero, one or more input parameters, and return a value of the type specified by the TResult parameter.
In my scenario, this particular method accepts Func which accepts an Employee and returns a bool value. For this, let’s create a method which I am going to point my Func to. Following method accepts an employee and checks whether his/her FirstName is equal to “Jaliya” and returns true or false.
static bool NameIsEqual(Employee emp)
return emp.FirstName == "Jaliya";
Func<Employee, bool> myFunc = new Func<Employee, bool>(NameIsEqual);
Console.WriteLine(employees.First(myFunc).FirstName);
Again with the use of Lambda Expressions, I can make my code simple.
Console.WriteLine(employees.First(e => e.FirstName == "Jaliya").FirstName);
Predicate<T> represents a method that defines a set of criteria and determines whether the specified object meets those criteria.
In here it’s a Predicate of type Employee. So let’s create a method which accepts a Employee and check whether he/she is born in “1986”. If yes, it will return true or else false.
static bool BornInNinteenEightySix(Employee emp)
return emp.Birthday.Year == 1986;
Now I am creating a Predicate pointing to above method.
Predicate<Employee> predicate = new Predicate<Employee>(BornInNinteenEightySix);
Console.WriteLine(employees.Find(predicate).FirstName);
Console.WriteLine(employees.Find(e => e.Birthday.Year == 1986).FirstName);
Now you must be wondering what is the difference between Func and Predicate. Basically, those are the same, but there is a one significant difference.
static string MyMethod(int i)
return "You entered: " + i;
Func<int, string> myFunc = new Func<int, string>(MyMethod);
Download Sample
Happy Coding.
Just a small remark:
employees.ForEach(empAction);
can also be replaced by
employees.ForEach(CalculateAge);
Congratulations for winning a TechNet Guru Gold Medal! blogs.technet.com/.../technet-guru-awards-january-2014.aspx
This article was highlighted in the TechNet Wiki Ninja Top Contributors weekly blog , Most Popular Article Award, 18/01/2014 - blogs.technet.com/.../top-contributors-awards-sharepoint-troubleshooting-mega-azure-pack-portal-gurus-c-and-well-lessons-learnt-o-o.aspx