Several inheritance functions are required in C #. What am I doing wrong?

class UDPClient { } class LargeSimulator { } class RemoteLargeSimulatorClient : UDPClient, LargeSimulator { } 

They say if you need multiple inheritance, your design is disabled.

How can I do this in C # without any changes?

+4
source share
6 answers

You can use only one base class in C #. However, you can implement as many interfaces as you want. Combine this with the advent of Extension Methods and you have a (hacker) job.

+5
source

C # allows only one inheritance, although you can inherit as many interfaces as you like.

You can choose only one class for inheritance and make the rest of the interfaces, or just make them all interfaces.

You can also link your inheritance as follows:

 class UDPClient { } class LargeSimulator : UDPClient { } class RemoteLargeSimulatorClient : LargeSimulator { } 
+4
source

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

Multiple class inheritance is not possible in C #

+1
source

Short answer: multiple inheritance is not allowed in C #. Reading on Interfaces: http://msdn.microsoft.com/en-us/library/ms173156.aspx

A slightly longer answer: maybe some other design template will suit you as a strategy template, etc. Inheritance is not the only way to achieve code reuse.

0
source
 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 member methods. Multiple inheritance does not exist in C #. However, you can implement several interfaces.

0
source

One possible substitute for multiple inheritance is mixins. Unfortunately, C # also does not have such, but workarounds are possible. Most rely on the use of extension methods (as suggested by the previous responder). 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/

0
source

All Articles