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?

3
  • looks like your design is off. Commented Jun 21, 2011 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? Commented Jun 21, 2011 at 18:55
  • are you sure that RemoteLargeSimulatorClient is UDPClient? Maybe you should use composition instead. Commented Jun 21, 2011 at 19:24

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

Comments

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
{
}

Comments

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#

Comments

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/

Comments

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.

Comments

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.

3 Comments

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
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.
@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". :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.