Specifying Properties in Interfaces - CSharp Custom Type

CSharp examples for Custom Type:interface

Introduction

The format for declaring a property within an interface is as follows:

modifier(s) datatype name
{
   get;
   set;
}

Demo Code

using System;//from   w  w  w  .  j a  v  a 2  s  .  co  m
public interface IShape
{
   int Sides
   {
      get;
      set;
   }
   double Area();
}
public class Square : IShape
{
   private int InSides;
   public  int SideLength;
   public double Area()
   {
      return ((double) (SideLength * SideLength));
   }
   public int Sides
   {
      get { return InSides; }
      set { InSides = value; }
   }
   public Square()
   {
      Sides = 4;
   }
}
public class Props
{
   public static void Main()
   {
      Square mySquare = new Square();
      mySquare.SideLength = 5;
      Console.WriteLine("\nDisplaying Square information:");
      Console.WriteLine("Area: {0}", mySquare.Area());
      Console.WriteLine("Sides: {0}", mySquare.Sides);
   }
}

Result


Related Tutorials