Concept of Delegates

abhishek kumarabhishek kumar
5 min read

What is Delegates?

First of all in our mind we have a question that there is small "delegate" and there is Capital "Delegate". What it is mean and why there is small and capital in terms of "delegate" and "Delegate".

Before resolving above question Lets first elaborate why Delegate?

Lets take an real scenario so that we can understand the basics:-

  1. Suppose in class we have multiple methods/functions that have same signature(means same return type, same numbers of datatype as a parameter).

  2. Now client request/call 1(one) function for performing task or for result, How he can call?.

  3. It can call by creating an object/instance of a class.

  4. Suppose, client only want to access Name of a company of a class Company like below example:-

  5. In above example as you can see:-

  6. The problem with above code is that, whenever, we write obj. , it gives not only CompName() function but it also gives CompAddress() function directly, which is not good for the security purpose.

  7. Now the question is, how we can eliminate the above issue, so that client can access the function as per their requirements.

  8. By above example we can also say that the Client class knows the internal mechanism or internal working of a company which is not good for the Company.

  9. The main problem is that client calling directly the functions/methods of a Company class, which is called tightly coupled.

  10. Tightly Coupled means, Client class is directly associated with a Company class and if there is change in Company class then there is also change in the Client class, which leads to complexity and time consuming.

  11. Now question arise how we can resolve the issue?....the answer is Delegate.

  12. Delegate not only provide loose coupling but also eliminate the direct communication between classes and also provide some rich type of functionalities through which coders can build applications, libraries in efficient manner with less complexity and without any halt.

Now lets talk about What Delegate is:-

  1. Capital "Delegate" is a abstract class, it means that for creating an object of abstract class we have to inherit the abstract class.

  2. Important:- "Delegate" abstract class is so special that can't be inherited means, we can't create an child class of "Delegate" abstract class.

  3. If we try to inherit we get compile time error.

  4. "Delegate" class provide a rich type of functionalities:-

  • Asynchronous Programming.

  • Event Pattern.

  • Lambda Programming.

  • Anonymous method.

  • Callback.

  • Structure tree.

  1. As you can see above, Delegate class have rich functionalities that every programmer needs to use.

  2. The question arise how the programmer use the above functionalities? , because "Delegate" class is special abstract class that can't be inherited and due to this object/instance can't be created.

  3. The answer is small "delegate".

Lets elaborate small "delegate":-

  1. "delegate" is a type which refer to or we can say points to functions/methods of a class.

  2. "delegate" acts like a mediator or middleman that helps in accessing the Capital "Delegate" functionalities.

  3. "delegate" not only helps in accessing the "Delegate" functionalities but also helps in achieving loose coupling.

  4. Important point:- whenever we make an object/instance of "delegate" it automatically become sealed class.

Lets proof whether small "delegate" become sealed class or not and also lets resolve the issue that are occurred in above Company class and Client Class example by defining "delegate" as a mediator:-

First of all we need to understand the rule of how we can declare delegate:-

  • Its signature must match with the methods/functions that we define in the class or vice versa. signature means:- return type, parameters and type of arguments must be same.

  • We can declare delegate before the class or within the class its a choice of a coder.

  • Name of delegate can be anything but must be logical and name should be after return type.

First proof of becoming sealed class:-

  • Write code in notepad and save as “ExDelegateBasic.cs” in a specific folder.

  • Open Developer Command Prompt and search the file (ExDelegateBasic.cs) and compile it:- csc EExDelegateBasic.cs.
  • After compilation...write “ildasm” in command prompt.

  • Open file in ildasm folder structure.

  • Search for the "CompMediater" delegate that we declare in the code and click on it.

  • After that click on first(.class private auto ansi sealed).

  • After that, you can see that "CompMediater" delegate became a sealed class which extends MulticastDelegate/Delegate class which is abstract as above image.

  • When an small "delegate" become as sealed class, it automatically become child class of Capital "MultiCastDelegate/Delegate" class as we proof in the above image.

  • Now going to resolving the issue that are occurred in above Company class and Client Class in below example:-

  • In above image-Line1, We have declared CompMediate(); which is a delegate type.

  • In class Company we have declare two(2) functions/methods, their signature must be same as delegate type(Line-1).

  • In client class we have created an object/instance (obj)of an class Company and also we have created an object/instance of an delegate type (compMediate).

  • Whenever the object of delegate type is declared with the help of “new” in line number 18, at that moment delegate type “CompMediater” become a sealed class and that sealed class stored in Heap memory.

  • whenever the delegate type become sealed class it automatically become child class of Delegate/MultiDelegate class.

  • As you can see in line number 18, we have passed the function/method of a Company class (obj.CompName) in CompMediater(). This is the proof or we can say this is the reason we call delegate type is a pointer/reference of an function/method of a class.

  • In line number 20 we write “CompMediater.Invoke()”. From Where the Invoke() function comes from?….it comes from Delegate abstract class.

  • When “CompMediater.Invoke()” calls, it automatically calls the CompName() functions and this function is accessed with the help of an CompMediater delegate type which pointing to CompName() function in line number 18.

  • By above we can say that Class Client not communicating directly to class Company. For communication class delegate type (CompMediater) acts as a mediator for class Client and class Company for accessing the function CompName(). This is where we are achieving loose coupling.

Now one more question arise, suppose Client also wants the Company information along with Company name as it defined in line numbers-4 and 8, How it will get these information’s?……Answer is MultiCastDelegate.

We will talk about MultiCastDelegate in next blog.

Hope you understand the concept of Delegates with small “delegate” and Capital “Delegate”.

If anyone have some points please share.

Thank your everyone….!!!

0
Subscribe to my newsletter

Read articles from abhishek kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

abhishek kumar
abhishek kumar

Serves 5+ years in IT field as a Website Administrator, E-Office Administrator, Web Developer, Full stack developer in different phase of Software Development Life Cycle(SDLC) including development, Implementation and testing.