Callback Operation By Delegate Or Interface

Introduction

A "callback" is a term that refers to a coding design pattern. In this design pattern executable code is passed as an argument to other code and it is expected to call back at some time. This callback can be synchronous or asynchronous. So, in this way large piece of the internal behavior of a method from the outside of a method can be controlled. It is basically a function pointer that is being passed into another function.
 
Delegate is a famous way to implement Callback in C#.  But, it can also be implemented by Interface. I will explain Callback by Delegate and Interface one by one. 

Callback by Delegate 
 
Delegate provides a way to pass a method as argument to other method. To create a Callback in C#, function address will be passed inside a variable. So, this can be achieved by using Delegate.
 
The following is an example of Callback by Delegate
  1. public delegate void TaskCompletedCallBack(string taskResult);  
  2. public class CallBack  
  3. {  
  4.     public void StartNewTask(TaskCompletedCallBack taskCompletedCallBack)  
  5.     {  
  6.         Console.WriteLine("I have started new Task.");  
  7.         if (taskCompletedCallBack != null)  
  8.             taskCompletedCallBack("I have completed Task.");  
  9.     }  
  10. }  
  1. public class CallBackTest  
  2. {  
  3.     public void Test()  
  4.     {  
  5.         TaskCompletedCallBack callback = TestCallBack;   
  6.         CallBack testCallBack = new CallBack();  
  7.         testCallBack.StartNewTask(callback);  
  8.     }    
  9.     public void TestCallBack(string result)  
  10.     {  
  11.         Console.WriteLine(result);  
  12.     }  
  13. }  
  1. static void Main(string[] args)  
  2. {  
  3.             
  4.      CallBackTest callBackTest = new CallBackTest();  
  5.      callBackTest.Test();  
  6.      Console.ReadLine();  
  7. }  
Output:

I have started new Task.
I have completed Task.
 
Delegate is a good way to implement Callback. But, you could use Interface for this. Because, suppose you have two methods - one for the success and another for the error and these methods will use Callback, so if you will use Delegate you will have to take two Delegates. 
 
If you need more than one Callback method then Callback mechanism with the use of Delegate doesn't makes sense. So, the use of Interface provides flexible and well-performing Callback mechanism for this scenario. 

Callback by Interface 
 
Use an Interface to provide Callback mechanism. It provides flexible and well-performing Callback mechanism. The following code example will elaborate the Callback mechanism with the help of Interface:
  1. public interface IMeeting  
  2. {  
  3.     void ShowAgenda(string agenda);  
  4.     void EmployeeAttendedMeeting(string employee);  
  5.     void EmployeeLeftMeeting(string employee);  
  6. }  
  1. public class Meeting : IMeeting  
  2. {  
  3.     public void ShowAgenda(string agenda)  
  4.     {  
  5.         Console.WriteLine("Agenda Details: " + agenda);  
  6.     }  
  7.  
  8.     public void EmployeeAttendedMeeting(string employee)  
  9.     {  
  10.         Console.WriteLine("Employee Attended Meeting: " + employee);  
  11.     }  
  12.   
  13.     public void EmployeeLeftMeeting(string employee)  
  14.     {  
  15.         Console.WriteLine("Employee Left Meeting: " + employee);  
  16.     }  
  17. }  
  1. public class MeetingRoom  
  2. {  
  3.     private string message;  
  4.     public MeetingRoom(string message)  
  5.     {  
  6.         this.message = message;  
  7.     }  
  8.  
  9.     public void StartMeeting(IMeeting meeting)  
  10.     {  
  11.         // Its a callback  
  12.         if (meeting != null) meeting.ShowAgenda(message);  
  13.   
  14.     }  
  15. }  
  1. public class MeetingExecution  
  2. {  
  3.     public void PerformMeeting()  
  4.     {  
  5.         IMeeting meeging = new Meeting();  
  6.         MeetingRoom meetingStarted = new MeetingRoom("Code Quality Improvement.");  
  7.         meetingStarted.StartMeeting(meeging);  
  8.     }  
  9. }  
  1. static void Main(string[] args)  
  2. {  
  3.     MeetingExecution meetingExecution = new MeetingExecution();  
  4.     meetingExecution.PerformMeeting();  
  5.             
  6.     Console.ReadLine();  
  7. }  
Output

Agenda Details: Code Quality Improvement.
 
Conclusion

Delegate is a great way to implement Callback operation. But, interface is good for the implementation of Callback in different scenario which I have mentioned above. Please read carefully and understand the code snippets. I have also attached code files. Writing code is good but code with proper design approach increases the quality of code. So, before writing any code always try to follow some design approach. Your code will be awesome.

Up Next
    Ebook Download
    View all
    FileInfo in C#
    Read by 9.7k people
    Download Now!
    Learn
    View all