Search code examples
c#interface

Trying to understand interfaces


I'm quite new in C# and struggles to understand the use and benefit of interfaces. I was given the example below (Example 1) to study the use of interface. My problem is that the program works as well without interface (Example 2). So what is the benefit of the interface in this example?

Example 1:

using System;

namespace ConsoleApp1
{
    interface IShape
    {
        double Area();
        double Perimeter();
    }

    class Circle : IShape
    {
        private double radius;
        public Circle(double radius)
        {
            this.radius = radius;
        }

        public double Area()
        {
            return Math.PI * radius * radius;
        }

        public double Perimeter()
        {
            return 2 * Math.PI * radius;
        }
    }

    class Rectangle : IShape
    {
        private double length;
        private double width;

        public Rectangle(double length, double width)
        {
            this.length = length;
            this.width = width;
        }

        public double Area()
        {
            return length * width;
        }

        public double Perimeter()
        {
            return 2 * ( length + width );
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            IShape circle = new Circle(5);
            IShape rectangle = new Rectangle(10, 20);;

            Console.WriteLine("Cirkelns area är {0}", circle.Area());
            Console.WriteLine("Cirkelns omkrets är {0}", circle.Perimeter());
            Console.WriteLine("Rektangelns area är {0}", rectangle.Area());
            Console.WriteLine("Rektangelns omkrets är {0}", rectangle.Perimeter());
        }
    }
}

Example 2:

using System;

namespace ConsoleApp1
{

    class Circle
    {
        private double radius;
        public Circle(double radius)
        {
            this.radius = radius;
        }

        public double Area()
        {
            return Math.PI * radius * radius;
        }

        public double Perimeter()
        {
            return 2 * Math.PI * radius;
        }
    }

    class Rectangle
    {
        private double length;
        private double width;

        public Rectangle(double length, double width)
        {
            this.length = length;
            this.width = width;
        }

        public double Area()
        {
            return length * width;
        }

        public double Perimeter()
        {
            return 2 * ( length + width );
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Circle circle = new Circle(5);
            Rectangle rectangle = new Rectangle(10, 20);

            Console.WriteLine("Cirkelns area är {0}", circle.Area());
            Console.WriteLine("Cirkelns omkrets är {0}", circle.Perimeter());
            Console.WriteLine("Rektangelns area är {0}", rectangle.Area());
            Console.WriteLine("Rektangelns omkrets är {0}", rectangle.Perimeter());
        }
    }
}

I studied and compered the two examples to try to understand what the interface is doing and why I shall use it


Solution

  • If you were interested in the sum of areas of n shapes, you can't just iterate over a list of mixed contents, like List<Circle Or Rectangle> ... so you will just use a List<IShape>. Then, you can just call the "area()" functions of whatever is in that list, if it implements the interface function.