What is Interface segregation principle and how to implement it in C#?

The Interface Segregation Principle (ISP) is the fourth principle of SOLID design principles. It states that clients should not be forced to depend upon interfaces that they don't use. Instead of creating one large interface with many methods, it's better to create multiple smaller, focused interfaces that serve specific purposes.

This principle promotes high cohesion and loose coupling by ensuring that classes only implement the methods they actually need, making the code more maintainable and flexible.

Syntax

Following is the syntax for creating segregated interfaces −

public interface ISpecificInterface {
   // Only methods related to specific functionality
   void SpecificMethod();
}

public class ConcreteClass : ISpecificInterface {
   public void SpecificMethod() {
      // Implementation
   }
}

Interface Segregation Principle ? Fat Interface IProduct ? ID, Weight, Stock ? Inseam, WaistSize ? HatSize Forces unused methods ? Segregated Interfaces IProduct + IPants + IHat ? Common: ID, Weight, Stock ? Pants: Inseam, WaistSize ? Hat: HatSize Implement only needed ? Better maintainability and flexibility

Before Interface Segregation

In this example, all classes are forced to implement properties they don't need. A baseball cap doesn't need Inseam or WaistSize, but it must implement them −

Example

using System;

public interface IProduct {
   int ID { get; set; }
   double Weight { get; set; }
   int Stock { get; set; }
   int Inseam { get; set; }
   int WaistSize { get; set; }
}

public class Jeans : IProduct {
   public int ID { get; set; }
   public double Weight { get; set; }
   public int Stock { get; set; }
   public int Inseam { get; set; }
   public int WaistSize { get; set; }
}

public class BaseballCap : IProduct {
   public int ID { get; set; }
   public double Weight { get; set; }
   public int Stock { get; set; }
   // These properties don't make sense for a hat
   public int Inseam { get; set; }
   public int WaistSize { get; set; }
   public int HatSize { get; set; } // Additional property needed
}

public class Program {
   public static void Main() {
      var jeans = new Jeans { ID = 1, Weight = 1.5, Stock = 50, Inseam = 32, WaistSize = 34 };
      var cap = new BaseballCap { ID = 2, Weight = 0.2, Stock = 100, HatSize = 7 };
      
      Console.WriteLine($"Jeans: ID={jeans.ID}, Inseam={jeans.Inseam}, WaistSize={jeans.WaistSize}");
      Console.WriteLine($"Cap: ID={cap.ID}, HatSize={cap.HatSize}");
      Console.WriteLine("Note: Cap forced to implement unused Inseam and WaistSize");
   }
}

The output of the above code is −

Jeans: ID=1, Inseam=32, WaistSize=34
Cap: ID=2, HatSize=7
Note: Cap forced to implement unused Inseam and WaistSize

After Interface Segregation

By segregating interfaces, each class implements only the properties it actually needs. This follows the ISP principle −

Example

using System;

public interface IProduct {
   int ID { get; set; }
   double Weight { get; set; }
   int Stock { get; set; }
}

public interface IPants {
   int Inseam { get; set; }
   int WaistSize { get; set; }
}

public interface IHat {
   int HatSize { get; set; }
}

public class Jeans : IProduct, IPants {
   public int ID { get; set; }
   public double Weight { get; set; }
   public int Stock { get; set; }
   public int Inseam { get; set; }
   public int WaistSize { get; set; }
}

public class BaseballCap : IProduct, IHat {
   public int ID { get; set; }
   public double Weight { get; set; }
   public int Stock { get; set; }
   public int HatSize { get; set; }
}

public class Program {
   public static void Main() {
      var jeans = new Jeans { ID = 1, Weight = 1.5, Stock = 50, Inseam = 32, WaistSize = 34 };
      var cap = new BaseballCap { ID = 2, Weight = 0.2, Stock = 100, HatSize = 7 };
      
      Console.WriteLine($"Jeans: ID={jeans.ID}, Inseam={jeans.Inseam}, WaistSize={jeans.WaistSize}");
      Console.WriteLine($"Cap: ID={cap.ID}, HatSize={cap.HatSize}");
      Console.WriteLine("Cap no longer needs to implement unused properties!");
   }
}

The output of the above code is −

Jeans: ID=1, Inseam=32, WaistSize=34
Cap: ID=2, HatSize=7
Cap no longer needs to implement unused properties!

Benefits of Interface Segregation

Before ISP (Fat Interface) After ISP (Segregated)
Classes implement unused methods Classes implement only needed methods
High coupling and low cohesion Low coupling and high cohesion
Changes affect all implementing classes Changes affect only relevant classes
Difficult to maintain and extend Easy to maintain and extend

Conclusion

The Interface Segregation Principle promotes creating multiple specific interfaces rather than one general-purpose interface. This ensures that classes only depend on methods they actually use, resulting in more maintainable, flexible, and loosely coupled code that adheres to SOLID design principles.

Updated on: 2026-03-17T07:04:36+05:30

411 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements