What is the relationship between LSP and Covariance and Contravariance? Is there any relationship? Is LSP a form of Covariance ?
+1
A:
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.
Jon Skeet
2010-10-10 08:50:27
LSP , says that Derived type can well be used instead of base types and Covariance interface allows its methods to return more derived types than those specified in the interface. These two statements looks similar to me that's causes me some confusion But Jon thanks for your answer
saurabh
2010-10-10 08:54:53
@saurabh: Well yes, but it was always the case that an `IEnumerable<Fruit>` sequence could contain `Banana` references, for example. I'm not really sure what you're getting at. Ultimately, it's a lot more important to understand what you can and can't do than decide the precise relationship between variance and LSP.
Jon Skeet
2010-10-10 08:57:01
@Jon : Sure , i will try to read more example and try to see the difference between the two.
saurabh
2010-10-10 09:01:24