In C#, basically Interface contains definitions for a group of related functionalities that a class or a struct can implement. So, before we begin, let's discuss the general definition of the interface in C#.

What is Interface?

A program has classes, methods, and properties. What if these members could be used in a unified way? What if we could share this code? 

Interface is a type which contains only the signatures of methods, delegates or events, it has no implementation.Implementation of the methods is done by the class that implements the interface.

Interfaces are more conceptual than abstract classes, since no method bodies are allowed at all. So an interface is kind of like an abstract class with nothing but abstract methods, and since there are no methods with actual code, there is no need for any fields. Properties are allowed though, as well as indexers and events.

Interface is a contract that defines the signature of the functionality. So if a class is implementing a interface it says to the outer world, that it provides specific behavior. Example if a class is implementing ‘Idisposable’ interface that means it has a functionality to release unmanaged resources.

You may also like to read : Abstract class in C#

Let's consider a complete  & simple C# example:

    interface IMainApp
     {
       //Function declared but not defined, by default Public
       int Sum(int i, int j); 
     }
     class MainApp: IMainApp
     {
        //Function is defined by the class which inherits a inteface
        //Must be declare Public
        public int sum(int i, int j) 
        {
           return i + j;
        }
     }

Where to use Interface in C#?

The main purpose of Interface to rectify the drawback of multiple inheritance, so the creators of C# have introduced a new concept called interfaces.

Before the introduction of Interface, there was no way to multiple inheritance in C#, which was already a possible way in a language like Java.

Properties Of Interface:-

  • Supports multiple inheritance(Single Class can implement multiple interfaces), so Multiple inheritance is possible with the help of Interfaces but not with classes.
  • Contains only incomplete method (Interfaces specify what a class must do and not how)
  • Can not Contain data members.
  • By Default interface members is public (We Can not set any access modifier into interface members).
  • Interface can not contain constructors.
  • Interfaces can’t have private members

Syntax of Interface

We declare interface by using the keyword interface which is as follows:-

public interface interface_name { }

Let's create another Interface example, which combines the two interface. This is the way to achieve the multiple inheritance.

using System;


namespace ConsoleApp2
{
    interface Interdemo
    {
        void Show();
    }

    interface Interdemo1
    {
        void Display();
    }

    interface CombineInherit : Interdemo, Interdemo1
    {
        //Above interfaces combined
    }
    class Program : CombineInherit
    {
        //Defination of methods here
        public void Show()
        {
            Console.WriteLine("Show() method Implemented");
        }

        public void Display()
        {
            Console.WriteLine("Display() method Implemented");
        }
        static void Main(string[] args)
        {
            Program inter = new Program();
            //call methods using Program instance
            inter.Show();
            inter.Display();
        }
    }
}

Output after debugging it as Console App in Visual Studio

inheritance-in-csharp-min.png

 Avoiding Name Ambiguity

Suppose you are declaring same method definitions in two different interfaces. The compiler will naturally show an error due to the ambiguity of the implemented method. Even if you use the "is" keyword, the compiler still will show warnings. To avoid this, you have to follow the syntax as below

Void <interface name>.<method name>
{
  //Body goes here
}

So complete example of this would be

using System;

interface Interdemo
{
  void Show();
}

interface Interdemo1
{
  void Show();
}


class Interclash:Interdemo,Interdemo1
{
  void Interdemo.Show()
  {
    Console.WriteLine("Show() method Implemented");
  }

  void Interdemo1.Show()
  {
    Console.WriteLine("Display() method Implemented");
  }

  public static void Main(string[] args)
  {
    Interclash inter = new Interclash();
    inter.Interdemo.Show();
    inter.Interdemo1.Show();
  }
}

Another Example of Interface similar to Multi-Level Inheritance

interface abc
    {
        void abc();
    }

    interface def : abc
    {
        void efd();
    }
    class Demo : def
    {
        public static void Main()
        {
            System.Console.WriteLine("Strart Interfaces");
            Demo refDemo = new Demo();
            def refdef = refDemo;
            refdef.abc();
            refdef.efd();
        }

        public void abc()
        {
            System.Console.WriteLine("In abc");
        }

        public void efd()
        {
            System.Console.WriteLine("In efd");
        }

    }

The above program compiles and runs successfully to produce a desired output. Interfaces support inheritance. Interface def inherits prototypes from interface abc. Class Demo implements the interface def. Interface variable refdef stores the reference to object of class Demo. Functions abc() and efd() of class Demo are invoked through interface reference variable refdef.

Output:

interface-in-csharp-min.png

That's it, we are done, let us know what do you think about this article, using the comments section.