Search code examples
c#-4.0covariancecontravarianceliskov-substitution-principle

Covariance and Contravariance with LSP


What is the relationship between LSP and Covariance and Contravariance? Is there any relationship? Is LSP a form of Covariance ?


Solution

  • I wouldn't say that Liskov's Substitution Principle is a "form" of covariance, so much as generic covariance allows LSP to be expressed in generic relationships. As of C# 4 / .NET 4, you can treat an IEnumerable<Banana> as an IEnumerable<Fruit> and should be able to do so without any nasty surprises - which is what LSP requires, basically.