Interface Segregation Principle (ISP) - SOLID Principles in c#

says clients shouldn't have to implement interfaces they don't utilise. To avoid clients relying on extraneous methods, interfaces should be split down into smaller, more focused interfaces.

According to Interface Segregation Principle (ISP) we should break huge interfaces into smaller, more focused interfaces so clients just need to implement their desired methods.

public interface IShape
{
   double Area();
   double Perimeter();
}

public class Circle : IShape
{
   public double Radius { get; set; }

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

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

public class Rectangle : IShape
{
   public double Width { get; set; }
   public double Height { get; set; }

   public double Area()
   {
       return Width * Height;
   }

   public double Perimeter()
   {
       return 2 * (Width + Height);
   }
}


The Interface Segregation Principle statesthat a client shouldn't have to implement an interface it doesn't need.


Our IShape interface has Area and Perimeter functions. Circle and Rectangle implement this interface and implement these methods.

We didn't use properties Radius. Width & Height  in IShape interface, as each shape has own properties

Author
Full Stack Developer

Deepak Talwar

Technical Architect & Full Stack Developer with 18+ years of Professional Experience in Microsoft Technologies & PHP Platform. Hands on experience with C#, VB, ASP.NET, ASP.NET MVC, ASP.NET Core, ASP.NET Web API, Linq, ADO.NET, Entity Framework, Entity Framework Core, Sql Server, MYSQL, NoSql, Javascript, Angular, jQuery, AWS, Azure, React, Angular, Laravel, Codeingiter, Serenity, VBA, Cloud Computing, Microservices, Design Patterns, Software Architecture, Web Design and Development.

Related Post