In this Chapter we will learn about Func<x,y> delegate and its real time implementations and will see how we can create centralize functions to filter generic list.

Basic Definition
Func delegates offer a straightforward approach to defining and utilising methods by passing them as parameters and returning them as results.
Func<>
is a delegate type for methods that return a value.- we can have up to 16 parameters, with the return type always being the last type parameter.
- It’s useful for passing methods as arguments or working with lambda expressions in a clean and concise way.
Syntax
Consider below syntax, it has two parts left and right. Right side of lamba expression is return value and Left side of lamba expression is input parameters.

Example: This is simple code
using System;
class Program
{
static void Main()
{
// A Func delegate that adds two integers
Func<int, int, int> add = (x, y) => x + y;
int result = add(5, 10); // Calls the function, adds 5 and 10
Console.WriteLine("Sum: " + result); /// result will be 15
}
}
Advance Examples
a. Filtering Generic List using Func<>
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
// Sample list of employees
List<Employee> employees = new List<Employee>
{
new Employee { Id = 1, Name = "Devesh", Age = 30, Department = "HR", Salary = 60000 },
new Employee { Id = 2, Name = "Pankaj", Age = 40, Department = "IT", Salary = 80000 },
new Employee { Id = 3, Name = "Kapil Kumar", Age = 35, Department = "IT", Salary = 75000 },
new Employee { Id = 4, Name = "Shiv", Age = 25, Department = "Finance", Salary = 50000 },
new Employee { Id = 5, Name = "Shanker", Age = 50, Department = "HR", Salary = 95000 }
};
// Define a list of Func delegates to filter employees
List<Func<Employee, bool>> filters = new List<Func<Employee, bool>>
{
// Filter by age (e.g., employees aged 30 or older)
emp => emp.Age >= 30,
// Filter by department (e.g., IT department)
emp => emp.Department == "IT",
// Filter by salary (e.g., employees with salary over 70000)
emp => emp.Salary > 70000
};
// Apply each filter sequentially
foreach (var filter in filters)
{
Console.WriteLine("Filtered Employees:");
var filteredEmployees = employees.Where(filter).ToList();
foreach (var emp in filteredEmployees)
{
Console.WriteLine($"Id: {emp.Id}, Name: {emp.Name}, Age: {emp.Age}, Department: {emp.Department}, Salary: {emp.Salary}");
}
Console.WriteLine();
}
}
}
Understanding code
1. We have created List<Func<Employee, bool>> filters means here return type will be bool and Input will be Employee object

2. We are adding below expressions to List.
emp => emp.Age >= 30,

i) emp => emp.Age >= 30 filters employees aged 30 or older.
ii) emp => emp.Department == “IT” filters employees who are in the IT department.
iii) emp => emp.Salary > 70000 filters employees with a salary greater than 70,000.
3 Apply Filters
using below code we are passing all filter conditions added above and here we will be able to filter employee list object.
// Apply each filter sequentially
foreach (var filter in filters)
{
Console.WriteLine("Filtered Employees:");
var filteredEmployees = employees.Where(filter).ToList();
foreach (var emp in filteredEmployees)
{
Console.WriteLine($"Id: {emp.Id}, Name: {emp.Name}, Age: {emp.Age}, Department: {emp.Department}, Salary: {emp.Salary}");
}
Console.WriteLine();
}
b. Passing Func<> as parameter
Let’s modify above example and have code below
static List<Employee> FilterEmployees(List<Employee> employees, Func<Employee, bool> filter)
{
return employees.Where(filter).ToList();
}
We have passed Func<> as parameter in above code now
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
// Sample list of employees
List<Employee> employees = new List<Employee>
{
new Employee { Id = 1, Name = "Devesh", Age = 30, Department = "HR", Salary = 60000 },
new Employee { Id = 2, Name = "pankaj", Age = 40, Department = "IT", Salary = 80000 },
new Employee { Id = 3, Name = "vipin", Age = 35, Department = "IT", Salary = 75000 },
new Employee { Id = 4, Name = "Shiv", Age = 25, Department = "Finance", Salary = 50000 },
new Employee { Id = 5, Name = "Shanker", Age = 50, Department = "HR", Salary = 95000 }
};
}
}
Apply Filter
a. Filter by Age
// Way 1
var emp=FilterEmployees(employees, emp => emp.Age > 30);
//Way 2
Func<Employee, bool> filterByAge = emp => emp.Age > 30;
var olderEmployees = FilterEmployees(employees, filterByAge);
b. Filter by Salary
//Way 1
var emp = FilterEmployees(employees, emp => emp.Salary > 70000);
//way 2
Func<Employee, bool> filterBySalary = emp => emp.Salary > 70000;
var emp = FilterEmployees(employees, filterBySalary);
Benefits of Func<> : Centralize function for filter
I found we can utilize Func<> as centralize code for filtering we can create centralise code for filter and we can pass conditions based on requirement
let’s compare filtering with out Func<>
Bad Code
List<Employee> employees = new List<Employee>
{
new Employee { Id = 1, Name = "Devesh", Age = 30, Department = "HR", Salary = 60000 },
new Employee { Id = 2, Name = "pankaj", Age = 40, Department = "IT", Salary = 80000 },
new Employee { Id = 3, Name = "vipin", Age = 35, Department = "IT", Salary = 75000 },
new Employee { Id = 4, Name = "Shiv", Age = 25, Department = "Finance", Salary = 50000 },
new Employee { Id = 5, Name = "Shanker", Age = 50, Department = "HR", Salary = 95000 }
};
var emp=employees.where(x=>x.age>89 && r=>r.Deaprtment="HR" && y=>y.Salary>30000)
Problem with this code
we need to update code every time when we need to update conditions and this code hard to maintain
var emp=employees.where(x=>x.age>89 && r=>r.Deaprtment="HR" && y=>y.Salary>30000)
Good Code or Code with Func<>
Creation of centralize function
static List<Employee> FilterEmployees(List<Employee> employees, Func<Employee, bool> filter)
{
return employees.Where(filter).ToList();
}
Here in this code we are passing Func<> and this will work based on condition passed as above.
Thanks happy coding
Comments
Post a Comment