Using Multiple Interfaces - CSharp Custom Type

CSharp examples for Custom Type:interface

Introduction

To implement multiple interfaces, you separate each interface with a comma.

class Square : IShape, IShapeDisplay
{
   ...
}

Demo Code

using System;// www.  j  a v a2 s .c o m
public interface IShape
{
   // Cut out other methods to simplify example.
   double Area();
   int Sides { get; }
}
public interface IShapeDisplay
{
   void Display();
}
public class Square : IShape, IShapeDisplay
{
   private int InSides;
   public  int SideLength;
   public int Sides
   {
      get { return InSides; }
   }
   public double Area()
   {
      return ((double) (SideLength * SideLength));
   }
   public double Circumference()
   {
      return ((double) (Sides * SideLength));
   }
   public Square()
   {
      InSides = 4;
   }
   public void Display()
   {
      Console.WriteLine("\nDisplaying Square information:");
      Console.WriteLine("Side length: {0}", this.SideLength);
      Console.WriteLine("Sides: {0}", this.Sides);
      Console.WriteLine("Area: {0}", this.Area());
   }
}
public class Multi
{
   public static void Main()
   {
      Square mySquare = new Square();
      mySquare.SideLength = 7;
      mySquare.Display();
   }
}

Result


Related Tutorials