Where did use delegates in your project - Part 1

Where did you use delegates in your project?
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

The Employee class has the following properties.
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

9 comments:

  1. Couldn't understand clearly.I can achieve the same without delegate i.e I will write
    Promote(Employee employee) methode in Employee class and call this methode in for loop to check whether employee is eligible or not.

    ReplyDelete
  2. 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.

    ReplyDelete
  3. I would use interface instead.

    ReplyDelete
  4. Venkat, 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..

    ReplyDelete
  5. Excellent venkat we have got it....

    ReplyDelete
  6. The link below shows how the delegate "EligibleToPromote" can be used to call a method that has some logic to promote the Employee.

    Delegate with an example

    ReplyDelete
  7. 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!

    ReplyDelete
  8. Could 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!

    ReplyDelete
  9. Totally 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.

    Thanks in Advance...

    ReplyDelete

If you are aware of any other C# questions asked in an interview, please post them below. If you find anything missing or wrong, please feel free to correct by submitting the form below.

 
Disclaimer - Terms of use - Contact Us | Internet Marketing