Delegates
Imagine we are writing an Employees Collection class which has methods to filter by first name and department. The code looks so similar, can we somehow parameterize it to remove the duplication?
using System; using System.Collections.Generic; namespace CodingArchitect.Demos.Delegates { public class EmployeesCollection { private readonly List<Employee> employees = new List<Employee>(); public EmployeesCollection FilterByFirstName(string firstName) { var matches = new EmployeesCollection(); foreach (var employee in employees) { if(string.Equals(employee.FirstName, firstName, StringComparison.InvariantCulture)) { matches.Add(employee); } } return matches; } public EmployeesCollection FilterByDepartment(string department) { var matches = new EmployeesCollection(); foreach (var employee in employees) { if (string.Equals(employee.Department, department, StringComparison.InvariantCulture)) { matches.Add(employee); } } return matches; } private void Add(Employee employee) { employees.Add(employee); } } public class Employee { public string FirstName { get; set; } public string LastName { get; set; } public string Department { get; set; } } }
Parameterize Method refactoring immediately comes to mind, only in this case the parameter should be a function which does the actual filtering. Passing functions as parameters is perfectly valid in C#. Now the condition alone can be passed as a delegate to the filter method. I am reusing the Predicate<T> delegate defined in the BCL. This is exactly what the FindAll method on the List<T> type in BCL does.
using System; using System.Collections.Generic; namespace CodingArchitect.Demos.Delegates { public class EmployeesCollection { private readonly List<Employee> employees = new List<Employee>(); public EmployeesCollection Filter(Predicate<Employee> condition) { var matches = new EmployeesCollection(); foreach (var employee in employees) { if (condition(employee)) { matches.Add(employee); } } return matches; } private void Add(Employee employee) { employees.Add(employee); } } public class Employee { public string FirstName { get; set; } public string LastName { get; set; } public string Department { get; set; } } }
The condition predicate can now be passed from the call site using a delegate (syntactic sugar like anonymous delegates, lambda expressions make it easier further).
Popular Posts
Popular posts
|
|
Threading Study Notes
|
|
Multi-Threading Basics – Deadlocks, Livelocks and Starvation |
|
Waiting for something to happen – Manual and AutoResetEvents |
|
|
|
SOA Study Notes
|
|
|
|
OOP Study Notes
|
|
|
|
CLR Generics Study Notes
|
|
CLR Generics – Comparing with other generics implementations |
|
|
|
Design Pattern Study Notes
|
|
|
|
Answers to Scott Hanselman’s interview questions
|
|
Scott Hanselman’s interview questions – Everyone who writes code |
|
Scott Hanselman’s interview questions – Mid-Level .NET Developer |
|
Scott Hanselman’s interview questions – Senior Developers/Architects |
|
Scott Hanselman’s interview questions – C# Component Developers |
|
|
|
Architecture Notes
|
|
|
|
CLR Study Notes
|
|