Delegates And Interface Overview

Introduction

Delegates and Interface are two distinct concepts in C# programming but they share common functionality in some cases.

Delegates and Interface are having the following common features:

  1. Both delegates and interfaces only include the declaration. Their implementation is done by a different programming object.

  2. Both enable a class designer to separate type declarations and implementation.

  3. Interface can be inherited and implemented by any class and a Delegate can be created for a method on any class, as long as the method suits the method signature of the delegate.

  4. An Interface or Delegate is being used by an object. Both have no knowledge of the class that implement.

  5. Basically, an Interface defines methods and properties and Delegate defines what a particular method can do.

So, a problem that is being solved by Delegate can also be solved with an Interface. I will explain this with some example.

Example of similarities between Delegate and Interface

The following example with explain the similarities between Delegate and Interface. Please read the following code carefully.

Using Delegate

  1. #region Delegate with Class    
  2.   
  3. //Defining Delegate.    
  4. public delegate void myDelegate();    
  5. public class MyClass    
  6. {    
  7.     public static void MyClassMethod()    
  8.     {    
  9.         Console.WriteLine("MyClassMethod is being executed");    
  10.     }    
  11. }    
  12.  
  13. #endregion  
Executing:
  1. static void Main(string[] args)    
  2. {    
  3.     //Initializing delegate and assigning method for the execution.    
  4.     myDelegate myDelegate = new myDelegate(MyClass.MyClassMethod);    
  5.     myDelegate();    
  6.     Console.ReadKey();    
  7. }  
Output:

MyClassMethod is being executed

Here, myDelegate only has the declaration. No implementation is provided for the delegate. However, this delegate is used to wrap any method matching its signature. So, it has wrapped MyClassMethod to the outside.

Using Interface
  1. #region Interface and Class    
  2. interface IMyInterface    
  3. {    
  4.     void MyMethodTest();    
  5. }    
  6.   
  7. public class MyClassTest : IMyInterface    
  8. {    
  9.     public void MyMethodTest()    
  10.     {    
  11.         Console.WriteLine("Executing MyMethodTest of MyClassTest");    
  12.     }    
  13. }    
  14.  
  15. #endregion   
Executing Method of MyClassTest
  1. static void Main(string[] args)    
  2. {    
  3.        
  4.     MyClassTest myClassTest = new MyClassTest();    
  5.     myClassTest.MyMethodTest();    
  6.     Console.ReadKey();    
  7. }   
Here, interface IMyInterface has a method called MyMethodTest which has only the declaration and not the definition or implementation code. This is very similar to the delegate.

So, now it is completely understood that an Interface reference or delegate can be used by an object that has no knowledge of the class that implements the interface or delegate methods.

When should Delegate be used in place of Interface

The following are the scenario or cases in which Delegate should be used in place of Interface:
  1. If Interface defines only a single method then we should use Delegate.
  2. If multicast is required.
  3. If subscriber need to implement the interface multiple times.

I will explain that if Interface has single method, then Delegate is very easy to use in place of Interface.

Using Interface

  1. #region Interface and Class    
  2. interface IMyInterface    
  3. {    
  4.     int MySum(int a, int b);    
  5. }    
  6.   
  7. public class MyClassTest : IMyInterface    
  8. {    
  9.     public  int MySum(int a, int b)    
  10.     {    
  11.         return a + b;    
  12.     }    
  13. }    
  14.  
  15. #endregion    
Executing Method
  1. static void Main(string[] args)    
  2. {    
  3.        
  4.    IMyInterface myClassTest = new MyClassTest();    
  5.    Console.WriteLine(myClassTest.MySum(10, 20));    
  6.     Console.ReadKey();    
  7. }  
Output

30

Using Delegate
  1. #region Delegate with Class    
  2.   
  3. //Defining Delegate.    
  4. public delegate int MyDelegate(int a, int b);    
  5. public class MyClass    
  6. {    
  7.     public static int Addition(int a, int b)    
  8.     {    
  9.         return a + b;    
  10.     }    
  11.   
  12. }    
  13.  
  14. #endregion  
Executing Method
  1. static void Main(string[] args)    
  2. {    
  3.   
  4.     MyDelegate myDelegate = MyClass.Addition;    
  5.     Console.WriteLine(myDelegate(10, 20));    
  6.   
  7.     Console.ReadKey();    
  8. }    
Output

30

So, Delegate made it smaller. But, it can be written in more optimized and smaller way like the following: 
  1. static void Main(string[] args)    
  2. {    
  3.   
  4.     Func<intintint> operation = (a, b) => a + b;    
  5.     Console.WriteLine(operation(10, 20));    
  6.   
  7.     Console.ReadKey();    
  8. }   
What are the differences between the Delegate and Event

There are so many differences between Delegate and Event. But, I will explain one practical difference between them.

The main difference is that different delegate instances can be provided for the same delegate from the same class. But, it did not happen with Interface.

I will elaborate this main difference with the help of following example:

Using Delegate:
  1. public delegate int MyDelegate(int a, int b);    
  2. public class MyClassResult    
  3. {    
  4.     //Below All methods can be used to implement the MyDelegate Delegate.    
  5.     void M1(int MyDelegate) { }    
  6.     void M2(int MyDelegate) { }    
  7.     void M3(int MyDelegate) { }    
  8.     void M4(int MyDelegate) { }    
  9. }   
Using Interface
  1. interface IMyInterfaceExample    
  2. {    
  3.     void MyResult(int a, int b);    
  4. }    
  5.    
  6. public class MyClassExample : IMyInterfaceExample    
  7. {    
  8.     public void MyResult(int a, int b)    
  9.     {    
  10.         // Only this method can be used to call an implementation through an interface    
  11.     }    
  12. }  
One more logical difference between Interface and Delegate is that Interface allows extending some object’s functionality, but delegates are just callbacks and function pointers.

How to add Delegate to an Interface?

I have the following problem statement related to Delegate for the Interface:

Problem statement:

I have one class “MyClass” that needs some Delegates. I want to use interface to remind me to not forget to set these delegates. How to do?

Solution

Firstly, all the purposes of Interface does not remind the wanted requirement or what do you want for the class. It is a means of abstraction. It’s the base of design in the Object Oriented Programming.

These are basically declaring delegate types, so they don’t belong in an Interface. You can use Event in Interface with Delegate type. 

Up Next
    Ebook Download
    View all
    Learn
    View all