Delegates Vs Interfaces in C# (C Sharp)

Delegates and Interfaces are two distinct concepts in C# but they share a commonality. Both delegates and interfaces only include the declaration. Implementation is done by a different programming object. To make it clear, consider the following two examples:


Example1: Using Delegate

delegate void sampleDelegate( );
class sampleClass1{
static void sampleMethod( ) {
Console.WriteLine(“Executing sampleMethod of sampleClass1”);
}
}
class sampleClass2 {
static void Main( ) {
sampleDelegate dele1 = new sampleDelegate(sampleClass1.sampleMethod);
dele1();
}
}

Example2: Using Interface

interface ITestInterface{
void sampleMethod( );
}
class sampleClass1 : ITestInterface {
void sampleMethod( ) {
Console.WriteLine(“Executing sampleMethod of sampleClass1”);
}
}
class sampleClass2 {
static void Main( ) {
sampleClass1 obj1 = new sampleClass1( );
obj1.sampleMethod( );
}
}

In Example1, sampleDelegate only has the declaration. No implementation is provided for the delegate. However, this delegate is used to wrap any methods matching its signature. In this example, delegate is used to wrap and execute sampleClass1.sampleMethod.

In Example2, interface ITestInterface has a method called sampleMethod which has only the declaration and not the definition or implementation code. This is very similar to the delegate. Implementation of sampleMethod is done in the class sampleClass1, for which the class has to implement the interface using the following line of code:

class sampleClass1 : ITestInterface {
You are actually overriding the interface method. After implementing the method, you are accessing it in Main( ) method of sampleClass2.

Both the examples intend in separating declaration from implementation of sampleMethod. And both these examples end up in the same output:
Executing sampleMethod of sampleClass1
But the examples are using two different concepts called delegate and interface. Now it is clear that both delegates and interfaces can be used to attain the same functionality. After understanding this similarity, following questions might occur in your mind:

• What is the difference between delegate and interface?
• When to use delegates and when to use interfaces?

Rest of this article will focus on answering these questions with the help of the comparison chart provided below:

Delegates Vs Interface in C#
Delegates & Interface in C Sharp

 

_______________________________________________________________________

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource

| All about Conceptual Analysis on .NET Remoting | Building desktop applications in .Net | Building Distributed Applications Efficiently Using .Net Remoting | C# (C Sharp) Unified Type System: Reference Types | C# (C Sharp) Unified Type System: Value Types | Data access in .Net | Delegates Vs Interfaces in C# (C Sharp) | How is Integration between Java and .NET Achieved | How is Versioning Achieved in C# (C Sharp) | Implementing Authorization and authentication in ASP.net | Implementing design patterns in .Net | List of Query Keywords in C# (C Sharp) |


| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.