Array Of Interface Type C# with Example



Array Of Interface Type C# with Example

 using System;

namespace CSharpInterface
{
	interface IShape
	{
		double Area();
	}
	
    class Shape
    {
        public double dim1, dim2;

        public Shape(double dimension1, double dimension2=0)
        {
            dim1 = dimension1;
            dim2 = dimension2;
        }
				
		public static void Display(IShape obj)
		{
			Console.WriteLine("Area of Shape : {0} ", obj.Area());
		}
    }
	
    class Rectangle : Shape, IShape
    {
        public Rectangle(double dimension1, double dimension2) : base(dimension1, dimension2) { }

        public double Area()
        {
            return dim1 * dim2;
        }
    }

    class Triangle : Shape, IShape
    {
        public Triangle(double dimension1, double dimension2) : base(dimension1, dimension2) { }

        public double Area()
        {
            return dim1 * dim2 / 2;
        }
    }
	
	class Circle : Shape, IShape
    {
        public Circle(double dimension1) : base(dimension1) { }

        public double Area()
        {
            return 22/7 * dim1 * dim1;
        }
    }

    class MultipleInterfaceImplementation
    {
        public static void Main(string[] args)
        {
            IShape[] InterfaceArray = {new Rectangle(6, 7), new Triangle(12, 23), new Circle(7)};
			
			foreach(IShape i in InterfaceArray)
				Shape.Display(i);
        }
    }
}
 

0 Comment's

Comment Form

Submit Comment