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:
- Both delegates and interfaces only include the declaration. Their implementation is done by a different programming object.
- Both enable a class designer to separate type declarations and implementation.
- 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.
- An Interface or Delegate is being used by an object. Both have no knowledge of the class that implement.
- 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
- #region Delegate with Class
-
-
- public delegate void myDelegate();
- public class MyClass
- {
- public static void MyClassMethod()
- {
- Console.WriteLine("MyClassMethod is being executed");
- }
- }
-
- #endregion
Executing: - static void Main(string[] args)
- {
-
- myDelegate myDelegate = new myDelegate(MyClass.MyClassMethod);
- myDelegate();
- Console.ReadKey();
- }
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 - #region Interface and Class
- interface IMyInterface
- {
- void MyMethodTest();
- }
-
- public class MyClassTest : IMyInterface
- {
- public void MyMethodTest()
- {
- Console.WriteLine("Executing MyMethodTest of MyClassTest");
- }
- }
-
- #endregion
Executing Method of MyClassTest - static void Main(string[] args)
- {
-
- MyClassTest myClassTest = new MyClassTest();
- myClassTest.MyMethodTest();
- Console.ReadKey();
- }
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:
- If Interface defines only a single method then we should use Delegate.
- If multicast is required.
- 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
- #region Interface and Class
- interface IMyInterface
- {
- int MySum(int a, int b);
- }
-
- public class MyClassTest : IMyInterface
- {
- public int MySum(int a, int b)
- {
- return a + b;
- }
- }
-
- #endregion
Executing Method- static void Main(string[] args)
- {
-
- IMyInterface myClassTest = new MyClassTest();
- Console.WriteLine(myClassTest.MySum(10, 20));
- Console.ReadKey();
- }
Output
30 Using Delegate
- #region Delegate with Class
-
-
- public delegate int MyDelegate(int a, int b);
- public class MyClass
- {
- public static int Addition(int a, int b)
- {
- return a + b;
- }
-
- }
-
- #endregion
Executing Method - static void Main(string[] args)
- {
-
- MyDelegate myDelegate = MyClass.Addition;
- Console.WriteLine(myDelegate(10, 20));
-
- Console.ReadKey();
- }
Output 30 So, Delegate made it smaller. But, it can be written in more optimized and smaller way like the following:
- static void Main(string[] args)
- {
-
- Func<int, int, int> operation = (a, b) => a + b;
- Console.WriteLine(operation(10, 20));
-
- Console.ReadKey();
- }
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:
- public delegate int MyDelegate(int a, int b);
- public class MyClassResult
- {
-
- void M1(int MyDelegate) { }
- void M2(int MyDelegate) { }
- void M3(int MyDelegate) { }
- void M4(int MyDelegate) { }
- }
Using Interface
- interface IMyInterfaceExample
- {
- void MyResult(int a, int b);
- }
-
- public class MyClassExample : IMyInterfaceExample
- {
- public void MyResult(int a, int b)
- {
-
- }
- }
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.