A delegate is a reference type, type-safe function(method) pointer. Delegates are used to invoke the method through the delegate instance. To invoke method,  delegates methods signature and return type must be same as that of a delegate.

You declare delegate similar to declaring the method:

How do we use delegates?

Lets create simple console application that will use method to display value:

This class does its intended yob nicely, but we can expand it by adding delegate. First we must declare delegate outside our class and instantiate our newly created delegate inside Main method then we can use our delegate to pass the value to our function.

Q: This look very complicated process just to invoke function. Why would i ever need to use delegates?

A: Yes it takes some extra work to create delegates but in next example you will see where delegates make much more sense.

Why do we use delegates?

Imagine that you need to create application for HR department and in that application you need to predict salary increase for employees that are employed full-time in that company. You would create application that behaves  like this:

Here we have Employee class that has necessary properties and  NextYearSalary method. NextYearSalary that takes List of Employees and checks each employee for his IsFullTime status, if that status is true then it multiplies employees years of service in company with 1% then multiplies it again with current salary. That way we get potential salary increase and print it to the console. We compile our program and ship it to all companies that use our application.

After some time we receive request from another company. Other company wants to see potential salary increase not for its full time employees but for those employees who have worked there for at least 3 years.

We could expand NextYearSllary method to accommodate that request or create another method only for that company, but these two approaches are not good solution for our problem because we always tend to have clean code and because second approach breaks DRY principle. Instead we will use delegates:

First wee took a look in NextYearSalary method. Inside our foreach loop we found if statement that needed to be changed. If statement is a boolean and checks Employee object so we created delegate that returns bool value and takes Employee object as a parameter. Now we needed to create method that matches that signature, so we created PotentialSalaryIncreaseForFullTimeEmployees method in our Main method. Next step was to expand signature of NextYearSalary method with SalaryIncreaseDelegate  delegate and replace condition of if statement with our delegates variable. Lastly, we call of NextYearSalary method in Main method with missing delegate variable.

Exercise 1:

Try to create method that:

  • can be used for SalaryIncreaseDelegate
  • can decide which Employees next years potential salaries will be displayed using NextYearSalary method for those employees that have worked in company at least 3 years.

Exercise 2:

Try to create delegate that will emulate NextYearSalary logic for expected salary:

Continue to Delegates part 2