Saturday, January 27, 2018

C# delegates - Importance & Usage




The purpose of this article is to drive you through c# delegates in very simple terms and with very simple examples. 
 
Understanding gap - People find it difficult because...

  1.   It’s confusing to read 
  2. Not sure of the real world usage of delegates.

I have gone through lot of sites, searching #simple #real_world #examples of delegate and found useful results but the only problem is I use to forget after sometime. It generally happens when you don’t know the need of delegates in your day to day work. Let’s take one oath here – 

“Whenever I need to communicate between lower class to upper class I will think of DELEGATES

Now the point is; when there is such kind of requirement you need to communicate from lower to upper class. -
Example: Let’s say you have a car and engine class (upper and lower class respectively). For starting the car, engine needs to be started and car wants the live status of the engine start process.
                            




Start engine can be called by creating an engine class object in car class and call its start method (something like engine.start()) but how we will notify about the engine-start-process run-time to the car class … #Delegates help me out here!!

Below example shows how it works. Don’t miss the communication between Car, delegate and engine mentioned on comments... :D

 

                      -----------------------------------------------------------------------------------
Output:




Lets put the above code like;



 Now we understood one most important usage of Delegates i.e.

1. Communication between two classes (specifically from lower to upper class) at run time (e.g. run time notification)

 Let's understand the 2nd most important usage. Below example is to promote an employee whose rank is greater than 4. We can achieve this by passing the employee details and check if its rank>4, Promoted. (Congratulations :D)



output:


 Now think of Employee as a framework class, which means it should not only define the generic (main) properties of the employee but also the methods should be generic and not specific. If we provide the above employee class to any company, this will work fine if company only wants to promote there employee on the basis of Rank. What if later on they change their mind and decided to promote employee on the basis of salary? I think changing in the framework class every time is a bad idea?
Again, DELEGATES can help us out here.

Note: I have tried to simplify the below example. Source - Kudvenkat delegate video series  

Lets make employee class such that, client will decide how to promote employee, framework class will just do the main processing over it and return the result to client.



output:


I hope you realize the 2nd important usage i.e.

2. To help creating framework method which is reusable and more generic.
  
In the above example, the promotion logic is written at the client side and can be change as per their requirement, but the main processing of promotion is only done in the framework class which will then confirm the employee is promoted.

I hope you understand the basic of delegates and help me learn more by providing your valuable suggestions and basic topics you want me write on...

Happy learning and Tip of the day...
"Lamda expression in c# internally creates a method for your delegate." 
which means you can get rid eligibleEmployee method from above example, the same can be written as;
 
  

No comments:

Post a Comment