Jump to content
  • Advertisement
Sign in to follow this  
Yratelev

[.net] multiple inheritance in c#

This topic is 5392 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, i have a bunch of classes that need to inherit more than one class. I know mulitple inheritance isnt allowed in c#, but wot other options are there? at the moment im using tons of interfaces and then duplicating the code in the classes, which is obviously not good any ideas, thanks, Yratelev

Share this post


Link to post
Share on other sites
Advertisement
The solution is simple, but painful: Restructure your design to remove the need for multiple base classes. As Magius suggests, you can set up type safety through interface inheritance, but since you've said that your concern is duplicating implementation code, this isn't going to help you. Ergo, you need to consider ways to work within the design idiom of C#. There are lots of resources on the web for software design & restructuring; typically MI is dealt with by refactoring to a composition usage pattern instead, wherein each independent type is a member of the base class.

Share this post


Link to post
Share on other sites
Does anyone else feel this is unreasonably... unreasonable? [smile] People "simulate" MI all the time using composition anyway, so why can't they just add it to the language? In some cases it *is* very useful (in particular if you need to merge hierarchies from legacy code) so why can't they just let us do the damned thing? They could put some restrictions (no diamond hierarchy, for instance) but at least give us the syntax sugar so we don't have to resort to composition.

Sorry for the rant. We now go back to scheduled programming.

Share this post


Link to post
Share on other sites
There is a blog that attempts to give a (very) short overview as to why they didn't include multiple inheritance in the CLR.

Magius

Share this post


Link to post
Share on other sites
There is one small detail you can use.

C# supports multiple inheritance.

What you've seen before looks like

public class MyClass : BaseClass
{
}

There is another trick you can use.

public class MyClass : BaseClass, InterfaceA, InterfaceB, InterfaceC
{
}

For those days that maybe you want some animal to be a hybrid cat&dog, but neither cat has a dog parent nor dog has a cat parent.

Share this post


Link to post
Share on other sites
tgraupmann,

<Picky>

That isn't true multiple inheritance though. C# doesn't support true multiple inheritance. It does support multiple interface inheritance. But in the case where you want to have a dog (that has an implementation, not just interface), a cat (that has an implementation, not just interface), and have one class inherit from both of them, then it isn't possible. I'm sure you know that, I'm just a picky bastard, no offense to you :).

</Picky>

With that said, I still have yet to see a true reason to use multiple inheritance that can't be done using another method (of course, this could be because I'm not a C++ nut). In my humble opinion, 98% of the time, true multiple inheritance just leads to hard-to-maintain, and hard-to-extend code (the spaghetti-code of the OOP world (the other 2% reserved for valid examples that I have been too close-minded to realize)).

However, I'm not a person that has had almost any experience with a true dynamic language like Smalltalk so I am perhaps not the person to say this. In .NET, I don't believe you even need true multiple inheritance though to be extremely successful as an architect.

On a different note, I would LOVE to be proved wrong :D. That way I can learn something knew that I didn't know before :D.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!