7

I would like to achieve this in C#

(Pseudocode)

class A;

class B : A;

class C : A, B;

...

A ac = (A)c;

...

B bc = (B)c;

Is this possible?

Dave Cluderay
  • 7,268
  • 1
  • 29
  • 28
neil
  • 131
  • 2
  • 9
  • Nope, check these out: http://stackoverflow.com/questions/191691/ http://stackoverflow.com/questions/995255/ – o.k.w Feb 13 '10 at 08:51
  • http://stackoverflow.com/questions/2846752/am-i-trying-to-implement-multiple-inheritance-how-can-i-do-this and http://stackoverflow.com/questions/2456154/does-c-support-multiple-inheritance – Russell Jul 08 '10 at 02:30

5 Answers5

9

You do not need multiple inheritance in this particular case: If class C inherits only from B, any instance of class C can be cast to both B and A; since B already derives from A, C doesn't need to be derived from A again:

class A      { ... }

class B : A  { ... }

class C : B  { ... }

...

C c = new C();
B bc = (B)c;    // <-- will work just fine without multiple inheritance
A ac = (A)c;    // <-- ditto

(As others have already said, if you need something akin to multiple inheritance, use interfaces, since a class can implement as many of those as you want.)

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
  • Arggh... I've overseen the obvious. Thanks for the hint! Regards – neil Feb 13 '10 at 10:36
  • Hmmm. One problem remains: I can't force C to behave as A. B enhances A and doesn't provide plain A functionalities anymore (as required). Simply spoken I need a C, which sometimes behaves as A and sometimes as B, depending on the context. – neil Feb 13 '10 at 19:14
3

No. C# does not support multiple inheritance of classes.

A class may inherit from one class, and may also implement multiple interfaces.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
2

It is not possible in C#, but, you should also think if this is the scenario you really want.

Is C really an A and a B ? Or does C has an A and a B. If the latter is true, you should use composition instead of inheritance.

Frederik Gheysels
  • 56,135
  • 11
  • 101
  • 154
  • I can't use composition for some reasons. Tried that. The problem: The base classes use delegates and providing references to itself in the callback. If C includes A and B the callbacks contain references to A and/or B, not to C as required. This is ensured if I inherit C from A and B. Interfaces are an option, use it already. – neil Feb 13 '10 at 19:12
0

you can use Interfaces for multiple inheritance.

masoud ramezani
  • 22,228
  • 29
  • 98
  • 151
0

C# doesn't support multiple inheritance, but as others have suggested you can use multiple interfaces instead. However, even with interfaces sometimes you want to reuse multiple interface implementations in the same class, which again would really require multiple inheritance.

To get around this (even though it's almost never needed) I've come up with a simulated multiple inheritance approach using a combination of partial classes and T4 Text Templates. It's a slight hack but it's better than copy/pasting the same interface implementation in lots of derived classes. Below is a simplified example:

IInterface.cs

public interface IInterface
{
    void Foo();
}

InterfaceImpl.cs

public partial class InterfaceImpl : IInterface
{
    public void Foo()
    {
        Console.WriteLine("Foo");
    }
}

BaseClass.cs

using System;

public class BaseClass
{
    public void Bar()
    {
        Console.WriteLine("Bar");
    }
}

DerivedClassA.cs

public partial class DerivedClassA : BaseClass, IInterface
{
    public void FooBar()
    {
        this.Foo();
        this.Bar();
    }
}

DerivedClassAInterfaceImpl.tt

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<# var codeText = System.IO.File.ReadAllText(this.Host.ResolvePath("InterfaceImpl.cs")).Replace("InterfaceImpl", "DerivedClassA"); #>
<#= codeText #>

DerivedClassB.cs

 public partial class DerivedClassB : BaseClass, IInterface
    {
        public void BarFoo()
        {
            this.Bar();
            this.Foo();
        }
    }

DerivedClassBInterfaceImpl.tt

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<# var codeText = System.IO.File.ReadAllText(this.Host.ResolvePath("InterfaceImpl.cs")).Replace("InterfaceImpl", "DerivedClassB"); #>
<#= codeText #>

It's a little annoying that this requires creating a tt file for each derived class that wants to use the interface implementation, but it still saves on copy/paste code if InterfaceImpl.cs is more than a few lines of code. It's also probably possible to just create one tt file and specify the name of all the derived partial classes and generate multiple files.

I've mentioned this approach in a similar question here.

Community
  • 1
  • 1
Scott Lerch
  • 2,620
  • 1
  • 23
  • 34