Before we begin, let's discuss the general definition of the interface.

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.
 
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.
 
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).
  • Contains only incomplete method.
  • 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.

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.