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 third principle with an example. I covered the first two principles in my previous articles.
The SOLID principles
There are five principles to follow to ensure our application meets the SOLID requirements. These are as below.
- Single Responsibility Principle (SRP)
- Open Closed Principle (OCP)
- Liskov Substitution Principle (LSP)
- Interface Segregation Principle (ISP)
- Dependency Inversion Principle (DIP)
The Liskov Substitution Principle (LSP)
The Liskov Substitution Principle (LSP) states that an instance of a child class must replace an instance of the parent class without affecting the results that we would get from an instance of the base class itself. 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 us create a new .NET Core 3.1 console application in Visual Studio 2019 Community Edition as below.


Inside this project, I have created a new class file “LiskovSubstitutionPrinciple”. In this file, I create the following base and child classes.
// Not following the Liskov Substitution Principle
public class AccessDataFile
{
public string FilePath { get; set; }
public virtual void ReadFile()
{
// Read File logic
Console.WriteLine($"Base File {FilePath} has been read");
}
public virtual void WriteFile()
{
//Write File Logic
Console.WriteLine($"Base File {FilePath} has been written");
}
}
public class AdminDataFileUser : AccessDataFile
{
public override void ReadFile()
{
// Read File logic
Console.WriteLine($"File {FilePath} has been read");
}
public override void WriteFile()
{
//Write File Logic
Console.WriteLine($"File {FilePath} has been written");
}
}
public class RegularDataFileUser : AccessDataFile
{
public override void ReadFile()
{
// Read File logic
Console.WriteLine($"File {FilePath} has been read");
}
public override void WriteFile()
{
//Write File Logic
throw new NotImplementedException();
}
} 
Anil KatariyaPosted Oct 16, 2024, 3:50 AM
Great explanation of the Liskov Substitution Principle! The example helps us understand how derived types should behave in a way that doesn’t break the base type’s contract. I especially liked how the code initially violated LSP, and then how it was refactored using interfaces to ensure compliance. This approach makes it clear why adhering to the solid principles is essential for building robust, scalable, and maintainable systems. I'm looking forward to the next post on the Dependency Inversion Principle!
maksud saifullahPosted Apr 3, 2022, 4:08 AM
This is ISP not LSP
Tonmoy StlPosted Nov 22, 2021, 12:12 PM
I think this related to ISP.
saravanakumar radhakrishnanPosted Jun 21, 2021, 5:54 PM
This is ISP