User Name:

User Email:

This information will only be saved for the purposes of communicating with those who have provided this information voluntarily regarding our services.We will never sell your name or email address to anyone.
© 2021 - First Crazy Developer (Abhishek Kumar)

crazydeveloper Decoupling with Delegates in C#

Hi friends, few days back we had discuss about the Delegate in C#. But I am still getting lots of mail, where they confused to explain about uses of Delegate in C#. Today we will try to understand what problem does Delegate solve, if we understand the problem domain then it will be so easy to explain the solution or we can say easy to map the delegate with problem and what Delegate all about in C#?

Today first we will understand the problem then step by step move ahead and discuss more details about Delegate to address the problems in C#.

Look at the following code:

  1. public class Math
  2. {
  3. public int Add(int Num1,int Num2)
  4. {
  5. return Num1 + Num2;
  6. }
  7. }
  8. static void Main(string[] args)
  9. {
  10. Math mathObj = new Math();
  11. int result = mathObj.Add(7, 16);
  12. }

In the above code we have a class named "Math" and in this class we have "Add" method. We can consume this Math class in UI code. In "Main" method we create object of Math class to call "Add" method. But after some period of times we need to change the code to add one more method in Math class named "Sub".

Look at the following code:

  1. public class Math
  2. {
  3. public int Add(int Num1,int Num2)
  4. {
  5. return Num1 + Num2;
  6. }
  7. public int Sub(int Num1, int Num2)
  8. {
  9. return Num1 - Num2;
  10. }
  11. }
  12. static void Main(string[] args)
  13. {
  14. Math mathObj = new Math();
  15. int result = mathObj.Add(7, 16);
  16. int subResult = mathObj.Sub(17,6);
  17. }

In the above code we can see any change in our business object "Math" class leads to code change in UI layer. Now we got a problem in our code and need to find the solution to resolve these types of problems. Here we have an important question "how can we resolve this problem in C#?"

Look at the following image:

In the above image we can see if somehow we can create a layer between UI layer (Client) and Business layer. Means there should be some abstract pointer who stand between both layers then we will be able to resolve this problem.

In other words if our UI layer rather than directly communicate with business objects like Add & Sub etc. methods it just references the abstract pointer. And then after base on provided input(s) abstract pointer internally call the required action.

Now with the help of Delegates we can create this abstract pointer. Basically Delegates are nothing just the pointer of functions or methods. We move ahead and understand how to create Delegates in C#?

Look at the following code:

  1. class Program
  2. {
  3. public delegate int pointToSPecificMethod(int Num1, int Num2); // 1. Declare Delegates
  4. static void Main(string[] args)
  5. {
  6. Math mathObj = new Math();
  7. pointToSPecificMethod myPointer = null; // 2. Create the reference of delegate
  8. myPointer = mathObj.Add; // 3. Point the method
  9. int result = myPointer.Invoke(7, 16); // 4. Invoke the delegate
  10. int subResult = mathObj.Sub(17,6);
  11. }
  12. }
  13. public class Math
  14. {
  15. public int Add(int Num1,int Num2)
  16. {
  17. return Num1 + Num2;
  18. }
  19. public int Sub(int Num1, int Num2)
  20. {
  21. return Num1 - Num2;
  22. }
  23. }

In the above code we can see we have an add function in Math class and we created a simple delegate to point this function. So with the help of given example we can say there are four steps required to implement the Delegates in C#.

1. Declaration the delegates
2. Create the instance of delegates.
3. Point the function or method with delegates.
4. Invoke the delegates.

In the above code we can see there is delegate keyword to use to create delegate and the return type & parameters should be same as defined function or method.  In the above code Add function return the integer type value and there are two integer type parameters so we defined int return type with delegate and also passed two integer type parameters. It is important that there should be proper return and input type with declaration of delegates. After declaration of delegates we need to create the reference of delegates. In the above code we create the reference of delegates named "myPointer". In the next line we pointed the reference of delegate with "Add" function. After this line finally we invoked the delegate.

Now after the above discussion I hope we can understood the basic concepts of delegates and also knew the complete steps to implement the delegates in C#. Move ahead and start discussion our problem. Here we have an important question "how can we use delegates to implement decoupling in C#?"

Look at the following code:

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. Math obj = new Math();
  6. var pointerRefAdd = obj.GetMyPointer(1);
  7. int resultAdd = pointerRefAdd.Invoke(10, 15);
  8. var pointerRefSub = obj.getMyPointer(2);
  9. int resultSub = pointerRefSub.Invoke(20, 15);
  10. }
  11. }
  12. public class Math
  13. {
  14. public delegate int pointToSPecificMethod(int Num1, int Num2);
  15. public pointToSPecificMethod GetMyPointer(int Action)
  16. {
  17. pointToSPecificMethod myPointer = null;
  18. switch(Action)
  19. {
  20. case 1:
  21. myPointer = this.Add;
  22. break;
  23. case 2:
  24. myPointer = this.Sub;
  25. break;
  26. }
  27. return myPointer;
  28. }
  29. private int Add(int Num1,int Num2)
  30. {
  31. return Num1 + Num2;
  32. }
  33. private int Sub(int Num1, int Num2)
  34. {
  35. return Num1 - Num2;
  36. }
  37. }

In the above code we can see there is class named "Math". We have two private method named "Add" & "Sub". We have also a public function "GetMyPointer" which return type is delegate type and integer type parameter. In this method we created the reference of delegate named "myPointer" and then base on provided input we pointed require function with that delegate and then returned that delegate. In UI layer we created the object of Math class then after we call the public function "GetMyPointer" and passed the value into that. In the above code we can see that now we don't need to care about method in business layer. We just pass the input and base on that input we can invoke the function.

Now if we add new logic in business logic we don't need to change any code at UI layer or we can say it doesn't effect on UI layer. For example if we add two new method "Multiply" & "Divide" in "Math" class. After this changes if we want to call this function we need to pass some different input when we will call the "GetMyPointer" function.After all of the above discussion I hope we understood how can we implement decoupling with delegates in C#?

Note: Most efficient way to implement decoupling in C# are:

1. IoC Container

2. DI Container

3. Service Locator

Interface is also popular to implement decoupling in C#. We will discuss all mentioned point in next articles.

Happy reading!!!

Abhishek Kumar

crazydeveloper Home Page 12 November 2015

Become a Fan