Interface in C#

C# interface.

Interface is special kind of thing in C# that is used to contain signatures of methods, interface is just like abstract classes, it can not be instantiated. Interface is used to achieve multiple inheritance which can not be achieve by class. It is used to achieve fully abstraction because it can not have method body, implementation of an interface must be provided by class or struct.

Syntax.

access_specifier interface interface_name
{
  //signature of methods.
}

Example.

using System;
namespace interface_program
{
    public interface Drawable
    {
      void draw();
    }
    public class Rectangle : Drawable
    {
       public void draw()
      {
        Console.WriteLine("Drawing rectangle....");
      }
    }
    public class Circle : Drawable
    {
       public void draw()
      {
        Console.WriteLine("Drawing circle....");
      }
    }
    public class TestInhterface
    {
      public static void Main()
      {
        Drawable d;
        d = new Rectangle();
        d.draw();
        d = new Circle();
        d.draw();
        Console.ReadKey();
      }
    }
}

Output.

Drawing rectangle….
Drawing circle….