By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,184 Members | 1,276 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,184 IT Pros & Developers. It's quick & easy.

Covariant Return Types, programming without them?

P: n/a
I keep running into the scenario below over and over again. Currently I get
around not having covariant return types by using an interface and explicit
property definitions which works to some extent, but causes other problems
that occur with explicit definitions and also leads to much more code that
is also more difficult to maintain. The new keyword is not even an option,
if used in the example below Offspring on a Dog when in the context of an
Animal would return null even the Dog class has populated the Dog _offstring
Array. The new keyword is also extremely difficult to debug when the above
null scenario is encountered. As a rule I do not use new, and it is my
opinion that the new keyword should never be used in this context.

My Questions are:

Shouldn't covariant return be part of csharp, I can think of no reason they
would ever cause a problem?

Is the class below a poor design and is there a better way to achieve the
same effect without using covariant return types, and preferably without
interfaces?

How do you overcome the lack of covariant return types?

Thanks,

Mike

public class Animal
{
public virtual Animal[] Offspring
{
get { return this._offspring }
}
private Animal[] _offspring = null;
}

public class Dog : Animal
{
public override Dog[] Offspring
{
get { return this._offspring }
}
private Dog[] _offspring = null;
}
Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
I have been researching this again and it looks like covariant return types
*won't* be in cssharp 2.0 so in the meantime I think this is the best
workaround and would like to hear any comments.

In the example below I have chosen to use the new keyword and even though
some boxing is involved as a rule I will always interact with the base
class, preventing the nasty null error that often occurs with new when you
do not rely on the base class to hold the object reference. I will just
have to live with the performance hit for now. I think this is the best
workaround for the lack of covariant return types, eventhough interfaces can
be used, they introduce other issues. I do not like casting of any kind and
see it as an invitation for runtime errors but this one should be fairly
safe until things get better.

I am also going to research moving to managed C++ at some point if csharp
does not start fully supporting polymorphism.

public class Animal
{
public virtual Animal[] Offspring
{
get { return this._offspring; }
set { this._offspring = value; }
}
private Animal[] _offspring = null;
}

public class Dog : Animal
{
public new Dog[] Offspring
{
get { return base.Offspring as Dog; }
set { base.Offspring = value; }
}
}
Nov 17 '05 #2

P: n/a

"Mike" <mi********@com.nospam> wrote in message
news:eU**************@TK2MSFTNGP12.phx.gbl...
I have been researching this again and it looks like covariant return types
*won't* be in cssharp 2.0 so in the meantime I think this is the best
workaround and would like to hear any comments.

In the example below I have chosen to use the new keyword and even though
some boxing is involved as a rule I will always interact with the base
class, preventing the nasty null error that often occurs with new when you
do not rely on the base class to hold the object reference. I will just
have to live with the performance hit for now. I think this is the best
workaround for the lack of covariant return types, eventhough interfaces
can
be used, they introduce other issues. I do not like casting of any kind
and
see it as an invitation for runtime errors but this one should be fairly
safe until things get better.


Ya, new is the best you are going to get here. Generics might help in 2.0,
but otherwise new is what you are going to have to use.
A generic example:

public abstract class Animal<T> where T : Animal<T>
{
public T[] Offspring
{
get {...}
}
}

public class Dog : Animal<Dog>
{

}
Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.