简体   繁体   中英

c# multiple inheritance

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?

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.)

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

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

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.

您可以使用Interfaces进行多重继承。

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM