How To Define A User Defined Interface And Its Implementation In Class Using C#

  1. using System;  
  2. //Interface definition  
  3. public interface Shape {  
  4.     void area();  
  5. }  
  6. public class Circle: Shape //implementing user defined interface  
  7.     {  
  8.         public void area() {  
  9.             Console.WriteLine("*** Calculating Area of Circle ***");  
  10.             Console.Write("Enter the Radius:");  
  11.             float r = float.Parse(Console.ReadLine());  
  12.             Console.WriteLine("Area of Circle = " + (3.142 * r * r));  
  13.         }  
  14.     }  
  15. public class Square: Shape {  
  16.     public void area() {  
  17.         Console.WriteLine("*** Calculating Area of Square ***");  
  18.         Console.Write("Enter the Length:");  
  19.         float side = float.Parse(Console.ReadLine());  
  20.         Console.WriteLine("Area of Square = " + (side * side));  
  21.     }  
  22. }  
  23. class arrinterfacetest {  
  24.     static void Main(string[] args) {  
  25.         Console.WriteLine("*** Array of Interface Test ***");  
  26.         Shape[] s = new Shape[2];  
  27.         s[0] = new Circle();  
  28.         s[1] = new Square();  
  29.         for (int i = 0; i < s.Length; i++) {  
  30.             s[i].area();  
  31.             Console.ReadKey();  
  32.         }  
  33.     }  
  34. }