Covariance Example – In progress – Not finished
This blog is about the issues with covariance. Your question should be:
- Why should I care?
- What are the limitations?
- What is it?
- Who needs it?
- How is it supported today?
- How will it be supported in C# 4.0?
To answer these questions we need a solid and compilable example. A lot of the blogs that I read did not give concrete examples and how c# 4.0 might fix them.
Here is what does work right now with C# 3.0
Humans want what they can't have
Looking at the code below we notie that the "Clone()" method can return an "object" instead of a "Car" object.
Issue?
This is not going to work.
Wouldn't it be nice to have a method public Mammal Clone() instead? It would give type-safety to all callers of the method.
But that's a feature, which is currently not implemented in the C# language.
The more general return type in the interface or a base class can not be narrowed on implementing or deriving classes.
The same problem arises in a number of cases and on generics, as we will see later on.
Comments
- Anonymous
March 22, 2009
PingBack from http://www.anith.com/?p=21581