Create and use the Interface - CSharp Custom Type

CSharp examples for Custom Type:interface

Description

Create and use the Interface

Demo Code

using System;/*from   w  ww . j  ava  2 s .  c om*/
public interface IShape
{
   double Area();
   double Circumference();
   int    Sides();
}
public class Circle : IShape
{
   public int x;
   public int y;
   public double radius;
   private const float PI = 3.14159F;
   public double Area()
   {
      double theArea;
      theArea = PI * radius * radius;
      return theArea;
   }
   public double Circumference()
   {
      return ((double) (2 * PI * radius));
   }
   public int Sides()
   {
      return 1;
   }
   public Circle()
   {
      x = 0;
      y = 0;
      radius = 0.0;
   }
}
public class Square : IShape
{
   public int side;
   public double Area()
   {
      return ((double) (side * side));
   }
   public double Circumference()
   {
      return ((double) (4 * side));
   }
   public int Sides()
   {
      return 4;
   }
   public Square()
   {
      side = 0;
   }
}
public class Shape
{
   public static void Main()
   {
      Circle myCircle = new Circle();
      myCircle.radius = 5;
      Square mySquare = new Square();
      mySquare.side = 4;
      Console.WriteLine("Displaying Circle information:");
      displayInfo(myCircle);
      Console.WriteLine("\nDisplaying Square information:");
      displayInfo(mySquare);
   }
   static void displayInfo( IShape myShape )
   {
      Console.WriteLine("Area: {0}", myShape.Area());
      Console.WriteLine("Sides: {0}", myShape.Sides());
      Console.WriteLine("Circumference: {0}", myShape.Circumference());
   }
}

Result


Related Tutorials