0

I'm quite new to C# but I have quite good C++ knowledge. From C++ I'm used that something like this works:

class Base1 {};
class Derived1 : Base1 {};
class Base2 {
   Base1 foo();
};
class Derived2 {
   Derived1 foo();
};

which I know under the term Covariance. The piece of code might miss something but I hope you get what I want to say. As I tried to produce something like that in C# with override and stuff, the Compiler complained that the type had to be the same. like this:

class Base1 {};
class Derived1 : Base1 {};
class Base2 {
   Base1 foo();
};
class Derived2 {
   Base1 foo();
};

Might that lead to any problems? Of course as I am the programmer of function foo I can assure there will always be a Derived1 returned. But whoever calls my function doesn't know that.

I have a project in which I have some Manager classes, for which I wanted to use an Interface IManager (it's my own class, not any from .NET if there exists any with that name), because they all have to implement a method Load() for example. Load() always returns a an Object, that is managed by that certain Manager. If every Manager implements IManager, all of them would get the Interface

ManagableObject Load();

Is that good, bad? Or even better, is there any workaround?

I also read that it worked with templates, but only like this I guess:

List<Base1> myList;
myList.push_back(new Derived1());

Or can I use List as return type in Base1 and List in class Derived2? That would be ok too.

Thanks and best regards, Expecto

Verena Haunschmid
  • 1,252
  • 15
  • 40
  • There is nothing wrong with your first set of class definitions (apart from requiring method implementations) - so I think you might be missing something in your explanation. What compiler error do you get? – RJ Lohan Jul 23 '12 at 05:08
  • C# actually supports covariance in various places; however _covariant return values_ are not supported, which I believe is what you're after here. Your samples are also currently broken with regards to showing the actual problem. – rjnilsson Jul 23 '12 at 05:55
  • @RJLohan in English it would be sth like: "The return value has to be ManagableObject to match the overwritten method IManager.Load()" – Verena Haunschmid Jul 23 '12 at 09:28
  • @cwan yes, I meant the return value, but I thought I made that clear, sorry – Verena Haunschmid Jul 23 '12 at 09:30

2 Answers2

0

You need to declare your method using the new keyword.

 class Derived2 {
  new Derived1 foo();
};
test1
  • 11
  • 1
  • Use the new modifier to explicitly hide a member inherited from a base class. To hide an inherited member, declare it in the derived class using the same name, and modify it with the new modifier. (http://msdn.microsoft.com/en-us/library/51y09td4%28v=vs.71%29.aspx#vclrfnew_newmodifier) it used when class base { void test(){}} class derived:base{ new void test(){}} (new is used only to hide base method, without new keyword compiler produces only warning message but code is compilable and executable) – ASpirin Jul 23 '12 at 06:01
0

That's what I meant: Does C# support return type covariance? but I didn't know to look for the term return type covariance

Community
  • 1
  • 1
Verena Haunschmid
  • 1,252
  • 15
  • 40