Algorithm


In C#, an interface is a way to define a contract for a class without specifying the implementation details. It allows you to declare a set of methods, properties, events, or indexers that a class must implement. Interfaces provide a mechanism for achieving multiple inheritance in C#.

Code Examples

#1 Implementing an Interface in C#

Code - C# Programming

using System;
namespace CsharpInterface {

  interface IPolygon {
    // method without body
    void calculateArea(int l, int b);

  }

  class Rectangle : IPolygon {

    // implementation of methods inside interface
    public void calculateArea(int l, int b) {

      int area = l * b;
      Console.WriteLine("Area of Rectangle: " + area);
    }
  }

  class Program {
    static void Main (string [] args) {

      Rectangle r1 = new Rectangle();
    
      r1.calculateArea(100, 200);

    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Area of Rectangle: 20000

#2 Implementing Multiple Interfaces in C#

Code - C# Programming

using System;
namespace CsharpInterface {

  interface IPolygon {
    // method without body
    void calculateArea(int a, int b);

  }

  interface IColor {

    void getColor();
  }
   
  // implements two interface
  class Rectangle : IPolygon, IColor {

    // implementation of IPolygon interface
    public void calculateArea(int a, int b) {

      int area = a * b;
      Console.WriteLine("Area of Rectangle: " + area);
    }

    // implementation of IColor interface
    public void getColor() {

      Console.WriteLine("Red Rectangle");
            
    }
  }

  class Program {
    static void Main (string [] args) {

      Rectangle r1 = new Rectangle();
    
      r1.calculateArea(100, 200);
      r1.getColor();
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Area of Rectangle: 20000
Red Rectangle

#3 Using reference variable of an interface in C#

Code - C# Programming

using System;
namespace CsharpInterface {

  interface IPolygon {
    // method without body
    void calculateArea(int l, int b);

  }

  class Rectangle : IPolygon {

    // implementation of methods inside interface
    public void calculateArea(int l, int b) {

      int area = l * b;
      Console.WriteLine("Area of Rectangle: " + area);
    }
  }

  class Program {
    static void Main (string [] args) {
       
      // using reference variable of interface
      IPolygon r1 = new Rectangle();
    
      r1.calculateArea(100, 200);
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Area of Rectangle: 20000

#4 Practical Example of Interface in C#

Code - C# Programming

using System;
namespace CsharpInterface {

  interface IPolygon {
    // method without body
    void calculateArea();

  }   
  // implements interface
  class Rectangle : IPolygon {

    // implementation of IPolygon interface
    public void calculateArea() {
      
      int l = 30;
      int b = 90;
      int area = l * b;
      Console.WriteLine("Area of Rectangle: " + area);
    }
  }

  class Square : IPolygon {

    // implementation of IPolygon interface
    public void calculateArea() {
      
      int l = 30;
      int area = l * l;
      Console.WriteLine("Area of Square: " + area);
    }
  }

  class Program {
    static void Main (string [] args) {

      Rectangle r1 = new Rectangle();  
      r1.calculateArea();

      Square s1 = new Square();  
      s1.calculateArea();
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Area of Rectangle: 2700
Area of Square: 900
Advertisements

Demonstration


C# Programming Example interface