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

 

| 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) |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

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

Copyright - © 2004 - 2024 - All Rights Reserved.