In my previous articles I wrote about Solid Principles in C#.
In this article, I am going to show you when and how to use the Interface Segregation Principle in C# with an example project. You can find the repository on GitHub.
The master branch shows the initial code used in the example. There are separate tags and branches for each of the all solid principles that you can review or download as well. Here are links you can use to jump to these tagged versions in your browser:
What is the Interface Segregation Principle (ISP) in C#?
The Interface Segregation Principle is the fourth of the SOLID design principles. Let us take a look at Robert C. Martin’s definition and try to analyse it.
Clients should not be forced to depend on methods that they do not use.
What’s a Client?
It’s the calling code. It’s the code that is interacting with an instance of the interface.
What does Interface mean in C# for the Interface Segregation Principle (ISP)?
In C# we have two kinds of interfaces described by the ISP.
- C# interface.
- Public or Accessible Interface of a C# class.
A type’s interface is whatever can be accessed by a client.
Ok nice description but in the end what do we have to do?
Prefer small and related interfaces to fat ones.