or
How did you use delegates in your project?
or
Usage of delegates in a Real Time Project?
| All Questions ASP.NET CSharp SQL Server WCF HR Interview Questions | |
| Didn't find what you are looking for? Search this site, for frequently asked ASP.NET, C#, SQL Server, WCF and HR Interview Questions | |
This is a very common c sharp interview question. Delegates is one of the very important aspects to understand. Most of the interviewers ask you to explain the usage of delegates in a real time project that you have worked on.
Delegates are extensively used by framework developers. Let us say we have a class called Employee as shown below.
Employee Class ![]() |
1. Id
2. Name
3. Experience
4. Salary
Now, I want you to write a method in the Employee class, which can be used to promote employees. The method should take a list of Employee objects as a parameter, and should print the names of all the employees who are eligible for a promotion. But the logic, based on which the employee gets promoted should not be hard coded. At times, we may promote employees based on their experience and at times we may promote them based on their salary or may be some other condition. So, the logic to promote employees should not be hard coded with in the method.
To achieve this, we can make use of delegates. So, now I would design my class as shown below. We also, created a delegate EligibleToPromote. This delegate takes Employee object as a parameter and returns a boolean. In the Employee class, we have PromoteEmpoloyee method. This method takes in a list of Employees and a Delegate of type EligibleToPromote as parameters. The method, then loops thru each employee object, and passes it to the delegate. If the delegate returns true, then them Employee is promoted, else not promoted. So, with in the method we have not hard coded any logic on how we want to promote employees.
![]() |
In Part 2 we will see how to consume, the Employee class that we have created.
| All Questions ASP.NET CSharp SQL Server WCF HR Interview Questions | |
| Didn't find what you are looking for? Search this site, for frequently asked ASP.NET, C#, SQL Server, WCF and HR Interview Questions | |


Couldn't understand clearly.I can achieve the same without delegate i.e I will write
ReplyDeletePromote(Employee employee) methode in Employee class and call this methode in for loop to check whether employee is eligible or not.
If you use Promote(Employee employee) method instead of the delegate EligibleToPromote(Employee EmployeeToPromote), you will have to hard code the logic based on which you want to promote the Employee. Where as when you use the delegate, you can define the logic in a seperate method and pass that method as a parameter to the delegate. So, if you look at the example, we have not hard coded the logic on how we want to promote the employee. Instead we just depend on the delegate's boolean value. So, if the method, the delegate is pointing to, returns true, we will promote the employee else we will not. So, the delegate in the above example, is allowing the users of the Employee class, to define their own logic on how they want to promote their employees.
ReplyDeleteI would use interface instead.
ReplyDeleteVenkat, it would be helpful If you can add a piece of code to show how this delegate is calling a method which has some logic to promote the Employee..
ReplyDeleteExcellent venkat we have got it....
ReplyDeleteThe link below shows how the delegate "EligibleToPromote" can be used to call a method that has some logic to promote the Employee.
ReplyDeleteDelegate with an example
A good example, however things are better than before with c# 4.0, there are lambda expressions and generic methods, do do the same job!
ReplyDeleteCould have been better! You should have shown how the method names are passed as parameters to the static function "PromoteEmployee", depending on the criteria that you had mentioned above!
ReplyDeleteTotally agreed on the way Venkat has explained in the comment. It was helpfull quite a bit. It would be great if there is a sample code how the employee would be promoted based on two different criterias. It would be enogh with just how to consume PromoteEmployee method using just 2 employees who would be promoted on 2 different criteria.
ReplyDeleteThanks in Advance...