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 Liskov Substitution 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 Liskov Substitution Principle (LSP) in C#?
The Liskov Substitution Principle is one of the SOLID design principles. We can always reuse the definition from Wikipedia but this time will be hard to fully understand…
Let Φ(x) be a property provable about objects x of type T. Then Φ(y) should be true for objects y of type S where S is a subtype of T.
We can transform this sentence in:
If S is a subtype of T, then objects of type T should be replaced with the objects of type S.
Which simply stated means that:
Subtypes must be substitutable for their base types.