4
class UDPClient
{
}

class LargeSimulator
{
}

class RemoteLargeSimulatorClient : UDPClient, LargeSimulator
{
}

The saying goes, if you need multiple inheritance, your design is off.

How would I get this done in C# without having to implement anything?

FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91
jaybny
  • 1,026
  • 2
  • 13
  • 29
  • looks like your design is off. – bzlm Jun 21 '11 at 18:38
  • edit: i want implementation inheritance not interface inheritance... yes, i know about interfaces.. im just wondering if c# is just a bunch of copy/paste... or is there a way to reuse code from 1 source? – jaybny Jun 21 '11 at 18:55
  • are you sure that RemoteLargeSimulatorClient *is* UDPClient? Maybe you should use composition instead. – empi Jun 21 '11 at 19:24

6 Answers6

5

C# only allows single inheritance, though you can inherit from as many interfaces as you wish.

You could pick just one class to inherit from, and make the rest interfaces, or just make them all interfaces.

You could also chain your inheritence like so:

class UDPClient
{
}

class LargeSimulator : UDPClient
{
}

class RemoteLargeSimulatorClient : LargeSimulator
{
}
Neil N
  • 24,862
  • 16
  • 85
  • 145
5

You can only inherent from a single base class in C#. However, you can implement as many Interfaces as you'd like. Combine this fact with the advent of Extension Methods and you have a (hacky) work-around.

Brandon Moretz
  • 7,512
  • 3
  • 33
  • 43
1

To get multiple inheritance the way you want it, you need to make your UDPClient and LargeSimulator interface instead of class.

Class multiple inheritance isn't possible in C#

Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
1

One possible substitute for multiple inheritance is mixins. Unfortunately C# doesn't have those either, but workarounds are possible. Most rely on the use of extension methods (as a previous answerer suggested). See the following links:

http://mortslikeus.blogspot.com/2008/01/emulating-mixins-with-c.html http://www.zorched.net/2008/01/03/implementing-mixins-with-c-extension-methods/ http://colinmackay.co.uk/blog/2008/02/24/mixins-in-c-30/

daveaglick
  • 3,600
  • 31
  • 45
0

The short answer: Multiple inheritance is not allowed in C#. Read up on interfaces: http://msdn.microsoft.com/en-us/library/ms173156.aspx

The slightly longer answer: Maybe some other design pattern would suit you, like the strategy pattern, etc. Inheritance isn't the only way to achieve code reuse.

FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91
0
interface ILARGESimulator
{
}

interface IUDPClient
{
}

class UDPClient : IUDPClient
{
}

class LargeSimulator : ILARGESimulator
{
}

class RemoteLargeSimulatorClient : IUDPClient, ILargeSimulator
{
    private IUDPClient client = new UDPClient();
    private ILARGESimulator simulator = new LARGESimulator();

}

Unfortunately you will need to write wrapper methods to the members. Multiple inheritance in C# does not exist. You can however implement multiple interfaces.

Marino Šimić
  • 7,318
  • 1
  • 31
  • 61
  • 1
    LargeSimulator has 100,000 events and methods (from an external lib)... UDPClient has low level networking stuff.. do i need to pick 1 and copy paste? seems rediculous.. for a real language – jaybny Jun 21 '11 at 18:46
  • The just don't use this. You seem to not need an object that joins both into one. Seems like overkill to join something that big. Maybe some design pattern is missing in the game. – Marino Šimić Jun 21 '11 at 18:48
  • @jaybny, if you'd told us more about what you're trying to do, we could have helped you better. Sounds like SOLID and composition would do you more good than interfaces, inheritance and whining about how C# is just "copy paste". :) – bzlm Jun 21 '11 at 19:35