Covariant return types?
-
Does someone know why C# doesn't have covariant return types? All I want to do is this
class GeneralItem
{
public abstract object GetContents();
}class StringItem : GeneralItem
{
public override string GetContents()
{...}
}I get a compiler error saying it doesn't like return types of overridden methods to be different. It's legal in C++, so why is it not available in C#? The workaround suggested is to provide an explicit interface implementation, but I don't have one defined already. Defining a new interface just doesn't seem right. It isn't available in C# 2.0 either. Any idea why? Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
Does someone know why C# doesn't have covariant return types? All I want to do is this
class GeneralItem
{
public abstract object GetContents();
}class StringItem : GeneralItem
{
public override string GetContents()
{...}
}I get a compiler error saying it doesn't like return types of overridden methods to be different. It's legal in C++, so why is it not available in C#? The workaround suggested is to provide an explicit interface implementation, but I don't have one defined already. Defining a new interface just doesn't seem right. It isn't available in C# 2.0 either. Any idea why? Regards Senthil _____________________________ My Blog | My Articles | WinMacro
Why don't you just have the return type object and then return a string inside the func? The PROgrammer Niklas Ulvinge aka IDK
-
Why don't you just have the return type object and then return a string inside the func? The PROgrammer Niklas Ulvinge aka IDK
Because users of StringItem would have to cast to a
string
. They already know they have aStringItem
instance, so why should they have to cast the return value ofGetContents
tostring
again? In my code, the user would first have to cast from GeneralItem to StringItem anyway. So he/she actually ends up casting twice. Regards Senthil _____________________________ My Blog | My Articles | WinMacro -
Because users of StringItem would have to cast to a
string
. They already know they have aStringItem
instance, so why should they have to cast the return value ofGetContents
tostring
again? In my code, the user would first have to cast from GeneralItem to StringItem anyway. So he/she actually ends up casting twice. Regards Senthil _____________________________ My Blog | My Articles | WinMacroI think you have to live with that... The PROgrammer Niklas Ulvinge aka IDK
-
I think you have to live with that... The PROgrammer Niklas Ulvinge aka IDK
I want to know why there are no covariant return types, I do understand that I've to live with it, (atleast until the next version of C# rolls out, whenever that is.) Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
I want to know why there are no covariant return types, I do understand that I've to live with it, (atleast until the next version of C# rolls out, whenever that is.) Regards Senthil _____________________________ My Blog | My Articles | WinMacro
Becouse if you use that func and think you got the parent, but got the child and it return a string when you wanted an object. PS. They could do it with only child's to the return type is able to be a return type (and for object, that's everything...) The PROgrammer Niklas Ulvinge aka IDK
-
Does someone know why C# doesn't have covariant return types? All I want to do is this
class GeneralItem
{
public abstract object GetContents();
}class StringItem : GeneralItem
{
public override string GetContents()
{...}
}I get a compiler error saying it doesn't like return types of overridden methods to be different. It's legal in C++, so why is it not available in C#? The workaround suggested is to provide an explicit interface implementation, but I don't have one defined already. Defining a new interface just doesn't seem right. It isn't available in C# 2.0 either. Any idea why? Regards Senthil _____________________________ My Blog | My Articles | WinMacro
Probably because Microsoft thought it would be confusing. Why would you want to do this ? If the overridden classes differ by return type, how will you know what you're getting back anyhow ? Christian Graus - Microsoft MVP - C++
-
Probably because Microsoft thought it would be confusing. Why would you want to do this ? If the overridden classes differ by return type, how will you know what you're getting back anyhow ? Christian Graus - Microsoft MVP - C++
I'm anyway going to get a string if I call GetContents, whether it is on GeneralItem (with runtime type = StringItem) or StringItem directly. For StringItem, I already know I'll get a string, so why do I have to cast again? For example
// Assuming item is a StringItem at runtime.
private void ProcessItem(GeneralItem item)
{
Console.WriteLine(item.GetContents()); // Perfectly okay.
}private void ProcessItem(StringItem item)
{
string s = (string)item.GetContents(); // Why cast to string?
}Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
Becouse if you use that func and think you got the parent, but got the child and it return a string when you wanted an object. PS. They could do it with only child's to the return type is able to be a return type (and for object, that's everything...) The PROgrammer Niklas Ulvinge aka IDK
It shouldn't be a problem, after all, a string "is-a" object, so the code that calls GetContents() on GeneralItem should be able to handle any type whose base class is object. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
I'm anyway going to get a string if I call GetContents, whether it is on GeneralItem (with runtime type = StringItem) or StringItem directly. For StringItem, I already know I'll get a string, so why do I have to cast again? For example
// Assuming item is a StringItem at runtime.
private void ProcessItem(GeneralItem item)
{
Console.WriteLine(item.GetContents()); // Perfectly okay.
}private void ProcessItem(StringItem item)
{
string s = (string)item.GetContents(); // Why cast to string?
}Regards Senthil _____________________________ My Blog | My Articles | WinMacro
You are thinking only in terms of string is a typeof object kind of way what, if the return types did not come from the same hierarchy at all eg. base class returns point your override returns string, this is a very difficult thing to keep track of especially if you want the compiler to throw errors in these kind of situations ...
-
You are thinking only in terms of string is a typeof object kind of way what, if the return types did not come from the same hierarchy at all eg. base class returns point your override returns string, this is a very difficult thing to keep track of especially if you want the compiler to throw errors in these kind of situations ...
The compiler doesn't allow that. If the overridding method's return type is different, it must be a derived class of the base class method's return type. So your case, (base class method returning Point, derived class method returning string) will not compile. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
Does someone know why C# doesn't have covariant return types? All I want to do is this
class GeneralItem
{
public abstract object GetContents();
}class StringItem : GeneralItem
{
public override string GetContents()
{...}
}I get a compiler error saying it doesn't like return types of overridden methods to be different. It's legal in C++, so why is it not available in C#? The workaround suggested is to provide an explicit interface implementation, but I don't have one defined already. Defining a new interface just doesn't seem right. It isn't available in C# 2.0 either. Any idea why? Regards Senthil _____________________________ My Blog | My Articles | WinMacro
There's some discussion here: http://blogs.msdn.com/cyrusn/archive/2004/12/08/278661.aspx[^] Kevin
-
There's some discussion here: http://blogs.msdn.com/cyrusn/archive/2004/12/08/278661.aspx[^] Kevin
Thanks. Yes, I did read that but it doesn't seem to answer why covariant return types are not available in C#. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
Thanks. Yes, I did read that but it doesn't seem to answer why covariant return types are not available in C#. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
There doesn't seem to be an answer but there's yet more discussion here. http://www.gotdotnet.com/Community/MessageBoard/Thread.aspx?id=308523[^] It looks like it's not in C# 2.0 either but they are supported with delegates. http://msdn2.microsoft.com/library/ms173174(en-us,vs.80).aspx[^] C++/CLI also doesn't support them. It should be possible to do this kind of thing though, given sufficient desire, because Eiffel .NET was able to map all its language features (including covariant return types, generics and multiple inheritance) onto .NET 1.1. Kevin