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?
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.
C# Interview Questions on Delegates
What is a delegate?
A delegate is a type safe function pointer. Using delegates
you can pass methods as parameters. To pass a method as a parameter, to a
delegate, the signature of the method must match the signature of the delegate.
This is why, delegates are called type safe function pointers.
What is the main use of delegates in C#?
Delegates are mainly used to define call back methods.
What do you mean by chaining delegates?
Or
What is a multicast delegate?
The capability of calling multiple methods on a single event
is called as chaining delegates. Let me give you an example to understand this
further.
1. Create a new asp.net web application
2. Drag and drop a button control and leave the ID as
Button1.
3. On the code behind file, add the code shown below.
When you click the Button now, both Method1 and Method2 will
be executed. So, this capability of calling multiple methods on a single event
is called as chaining delegates. In the example, we are using EventHandler
delegate, to hook up Method1 and Method2 to the click event of the button
control. Since, the EventHandler delegate is now pointing to multiple methods,
it is also called as multicast delegate.
Will the following code compile?
No, the code does not compile. For the code to compile, the
signature of Method1 should match the signature of SampleDelegate.
What are the advantages of using interfaces
This is the most commonly asked interview question. This
interview question is being asked in almost all the dot net interviews. It is
very important that we understand all the concepts of interfaces and abstract
classes.
Interfaces are very powerful. If properly used, interfaces
provide all the advantages as listed below.
1. Interfaces allow us to implement polymorphic behaviour.
Ofcourse, abstract classes can also be used to implement polymorphic behaviour.
2. Interfaces allow us to develop very loosely coupled
systems.
3. Interfaces enable mocking for better unit testing.
4. Interfaces enables us to implement multiple class
inheritance in C#.
5. Interfaces are great for implementing Inverson of Control
or Dependancy Injection.
6. Interfaces enable parallel application development.
No comments:
Post a Comment