473,324 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

return Inherited class instead of "original" class

Hi,

why doesn't this compile:
class Program
{
public static void Main()
{
ClassA classA = new ClassA();

classA.DoSomethingOnA().DoSomethingOnB();
}
}

class ClassA : InterfaceA
{
public ClassB DoSomethingOnA() // <-- Error
{
Console.WriteLine("DoSomethingOnA");
return (new ClassB());
}
}

class ClassB : InterfaceB
{
public void DoSomethingOnB()
{
Console.WriteLine("DoSomethingOnB");
}
}

interface InterfaceA
{
InterfaceB DoSomethingOnA();
}

interface InterfaceB
{
void DoSomethingOnB();
}
ClassB is always InterfaceB, so in my opinion it should compile.

Thx

Jul 12 '06 #1
2 1029
Karsten Schramm wrote:
why doesn't this compile:
<snip>

Because C# (and .NET in general? not sure) doesn't support covariance
of return types for interfaces. I agree it's a bit of a pain.

Jon

Jul 12 '06 #2
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote:
Karsten Schramm wrote:
why doesn't this compile:

<snip>

Because C# (and .NET in general? not sure) doesn't support covariance
of return types for interfaces. I agree it's a bit of a pain.
Yes, the CLR doesn't support covariance on the return type of the method
when implementing an interface. One way around it is to explicitly
implement the interface, and have the covariant version public. That
way, you can delegate to the public version in the explicit
implementation.

-- Barry

--
http://barrkel.blogspot.com/
Jul 12 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
by: Noam Raphael | last post by:
Hello, I thought about a new Python feature. Please tell me what you think about it. Say you want to write a base class with some unimplemented methods, that subclasses must implement (or...
9
by: monkey | last post by:
I just learn to make a blank windows frame with python and wxpython. I found the statment "import wx" cannot work as the original "from wxPython.wx import *". I see in the readme file of wxpython...
2
by: Jeff Levinson [mcsd] | last post by:
I guess I would have to know what you mean by "not being able to edit the forms". Does this mean you get an error in the designer when you try to display an inherited form? Does this mean the...
9
by: Brian Henry | last post by:
If i inherite a queue class into my class, and do an override of the enqueue member, how would i then go about actually doing an enqueue of an item? I am a little confused on this one... does over...
4
by: John Richardson | last post by:
I haven't played much with Reflection before, so I'm hoping I'm missing something simple with this problem I'm having. Trying to follow the online examples for resizing a DataGrid row height, and...
18
by: sd2004 | last post by:
could someone please show/help me to copy all element from "class dog" to "class new_dog" ? Note: "class new_dog" has new element "age" which should have value "my_age"...
9
by: | last post by:
Execuse me. I do boxing, for example Employee class, as object: public object ManipulateObject(object objInput) { ... Object obj; obj = objInput; ...
3
by: OutdoorGuy | last post by:
Greetings, I have a "newbie" question. I was wondering if there is anything in C# that corresponds to VB's "Left" function? I simply want to retrieve the leftmost characters of a string and I...
10
by: Raj | last post by:
I need a VB function to return array of collections like Private Type Employee empname as string address as string salary as integer deptno as integer End Type dim employees() as Employee
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.