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
- //Defining Delegate.
- public delegate void myDelegate();
- public class MyClass
- {
- public static void MyClassMethod()
- {
- Console.WriteLine("MyClassMethod is being executed");
- }
- }
- #endregion
- static void Main(string[] args)
- {
- //Initializing delegate and assigning method for the execution.
- myDelegate myDelegate = new myDelegate(MyClass.MyClassMethod);
- myDelegate();
- Console.ReadKey();
- }
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
- static void Main(string[] args)
- {
- MyClassTest myClassTest = new MyClassTest();
- myClassTest.MyMethodTest();
- Console.ReadKey();
- }
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
Priyaranjan K SPosted Oct 29, 2015, 12:51 PM
Thanks for the Share
Former memberPosted Oct 29, 2015, 2:52 AM
thanks
Pramod GuptaPosted Oct 29, 2015, 1:33 AM
nice
Nagaraj SPosted Oct 28, 2015, 11:31 AM
"Using Inerface" Spelling wrong. Except that article is good
Former memberPosted Oct 26, 2015, 1:39 PM
Thanks
Sibeesh VenuPosted Oct 26, 2015, 1:19 AM
Nice Share
Harshad PansuriyaPosted Oct 26, 2015, 12:30 AM
Nice one
Humayun Kabir MamunPosted Oct 25, 2015, 11:30 PM
Nice...
Former memberPosted Oct 25, 2015, 10:37 PM
Thanks
Nilesh JadavPosted Oct 25, 2015, 9:43 PM
Nice one sir !!
Mukesh KumarPosted Oct 25, 2015, 2:58 PM
Great Work
Former memberPosted Oct 25, 2015, 10:33 AM
Thanks
Santhakumar MunuswamyPosted Oct 25, 2015, 10:30 AM
Good Article