Introduction
C# is an object-oriented programming language. These days whenever you talk about object-oriented programming you hear the acronym, SOLID. These are five design principles introduced by Michael Feathers to make our object-oriented applications easy to understand, maintain, and expand as future requirements change. Today, we will look at the fourth principle with an example. I covered the first three principles in my previous articles.
The SOLID principles
There are five principles to follow to ensure our application meets the SOLID requirements. These are shown below:
- Single Responsibility Principle (SRP)
- Open-Closed Principle (OCP)
- Liskov Substitution Principle (LSP)
- Interface Segregation Principle (ISP)
- Dependency Inversion Principle (DIP)
The Interface Segregation Principle (ISP)
The Interface Segregation Principle (ISP) states that a class must not have to implement any interface element that is not required by the particular class. This will ensure the class and ultimately the whole application is very robust and easy to maintain and expand if required. Let us look at this with an example:
Let's create a new .NET Core 3.1 console application in Visual Studio 2019 Community Edition, as shown below:


Inside this project, I have created a new class file “InterfaceSegregationPrinciple”. In this file, I created the following interfaces and classes.
- // Not following the Interface Segregation Principle
- public interface IWorker
- {
- string ID { get; set; }
- string Name { get; set; }
- string Email { get; set; }
- float MonthlySalary { get; set; }
- float OtherBenefits { get; set; }
- float HourlyRate { get; set; }
- float HoursInMonth { get; set; }
- float CalculateNetSalary();
- float CalaculateWorkedSalary();
- }
- public class FullTimeEmployee : IWorker
- {
- public string ID { get; set; }
- public string Name { get; set; }
- public string Email { get; set; }
- public float MonthlySalary { get; set; }
- public float OtherBenefits { get; set; }
- public float HourlyRate { get; set; }
- public float HoursInMonth { get; set; }
- public float CalculateNetSalary() => MonthlySalary + OtherBenefits;
- public float CalaculateWorkedSalary() => throw new NotImplementedException();
- }
- public class ContractEmployee : IWorker
- {
- public string ID { get; set; }
- public string Name { get; set; }
- public string Email { get; set; }
- public float MonthlySalary { get; set; }
- public float OtherBenefits { get; set; }
- public float HourlyRate { get; set; }
- public float HoursInMonth { get; set; }
- public float CalculateNetSalary() => throw new NotImplementedException();
- public float CalaculateWorkedSalary() => HourlyRate * HoursInMonth;
- }

Join the conversation! Your thoughts help the community grow.