Open In App
Related Articles

Delegates vs Interfaces in C#

Improve Article
Save Article
Like Article

A Delegate is an object which refers to a method or you can say it is a reference type variable that can hold a reference to the methods. Delegates in C# are similar to the function pointer in C/C++. It provides a way which tells which method is to be called when an event is triggered. Example: 


// C# program to illustrate Delegates
using System;
class GFG {
    // Declaring the delegates
    public delegate void AddVal(int x, int y);
    public void SMeth(int x, int y)
        Console.WriteLine("[{0} + {1}] = [{2}]",x,y, x + y);
    // Main Method
    public static void Main(String[] args)
        // Creating the object of GFG class
        GFG o = new GFG();
        // Creating object of delegate
        AddVal obj = new AddVal(o.SMeth);
        // Pass the values to the method
        // Using delegate object
        obj(190, 70);


[190 + 70] = [260]

Like a class, Interface can have methods, properties, events, and indexers as its members. But interfaces will contain only the declaration of the members. The implementation of the interface’s members will be given by the class who implements the interface implicitly or explicitly. Example: 


// C# program to illustrate the
// concept of interface
using System;
// A simple interface
interface inter {
    // method having only declaration
    // not definition
    void display();
// A class that implements the interface
class Geeks : inter {
    // providing the body part of function
    public void display()
        Console.WriteLine("Welcome to GeeksforGeeks..!");
    // Main Method
    public static void Main(String[] args)
        // Creating object
        Geeks o = new Geeks();
        // calling method


Welcome to GeeksforGeeks..!

Below are some differences between the Delegates and Interfaces in C#:

It could be a method only.It contains both methods and properties.
It can be applied to one method at a time.If a class implements an interface, then it will implement all the methods related to that interface.
If a delegate available in your scope you can use it.Interface is used when your class implements that interface, otherwise not.
Delegates can me implemented any number of times.Interface can be implemented only one time.
It is used to handling events.It is not used for handling events.
It can access anonymous methods.It can not access anonymous methods.
When you access the method using delegates you do not require any access to the object of the class where the method is defined.When you access the method you need the object of the class which implemented an interface.
It does not support inheritance.It supports inheritance.
It can wrap static methods and sealed class methodsIt does not wrap static methods and sealed class methods..
It created at run time.It created at compile time.
It can implement any method that provides the same signature with the given delegate.If the method of interface implemented, then the same name and signature method override.
It can wrap any method whose signature is similar to the delegate and does not consider which from class it belongs.A class can implement any number of interfaces, but can only override those methods which belongs to the interfaces.

Last Updated : 05 Sep, 2023
Like Article
Save Article
Similar Reads