Interface Reference As Parameter And Return Value C# with Example



Interface Reference As Parameter And Return Value C# with Example

 using System;

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

        public Shape(double dimension1, double dimension2)
        {
            dim1 = dimension1;
            dim2 = dimension2;
        }
				
		public static void Display(string type, IShape obj)
		{
			Console.WriteLine("Area of {0} : {1} ", type, 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 MultipleInterfaceImplementation
    {
        public static void Main(string[] args)
        {
            Rectangle newRectangle = new Rectangle(6, 7);
            Triangle newTriangle = new Triangle(12, 23);

            Shape.Display("Rectangle", newRectangle);
            Shape.Display("Triangle", newTriangle);
        }
    }
}
 

0 Comment's

Comment Form

Submit Comment