.NET 4 introduces covariance. I guess it is useful. After all, MS went through all the trouble of adding it to the C# language. But, why is Covariance more useful than go
Consider an API which asks for an IContainer
:
public void DrawShape(IContainer container>) { /* ... */ }
You have a Container
. How can you pass your container to the DrawShape
API? Without covariance, the type Container
is not convertible to IContainer
, requiring you to rewrap the type or come up with some other workaround.
This is not an uncommon problem in APIs that use a lot of generic parameters.