A thorough guide to the Liskov Substitution Principle (LSP) in C#, covering behavioral contracts (preconditions, postconditions, invariants) rather than just type signatures. Uses the classic Rectangle/Square violation to illustrate how mutable inheritance breaks postconditions, then shows fixes via shared interfaces and immutable records. Also covers NotImplementedException as an LSP signal, interface segregation as a remedy, and how structural patterns like Proxy, Decorator, Iterator, and Chain of Responsibility all depend on LSP correctness. Practical design rules include favoring sealed classes, immutability, small interfaces, documented contracts, and running base-type tests against all subtypes.

15m read timeFrom devleader.ca
Post cover image
Table of contents
Behavioral Subtyping: Contracts, Not Just Type SignaturesThe Classic Violation: Rectangle and SquareHow to Fix the Rectangle/Square ProblemNotImplementedException as an LSP SignalPreconditions and Postconditions in Plain EnglishDesigning LSP-Compliant Hierarchies in .NET 10LSP and the Proxy/Decorator PatternsFAQConclusion: Applying liskov substitution principle c# Correctly
363 Impressions