Covariance Example – In progress – Not finished

 

Covariance Example – In progress – Not finished

  • Comments 1

This blog is about the issues with covariance. Your question should be:

  1. Why should I care?
  2. What are the limitations?
  3. What is it?
  4. Who needs it?
  5. How is it supported today?
  6. 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.

 

 

Page 1 of 1 (1 items)
Leave a Comment
  • Please add 5 and 7 and type the answer here:
  • Post