Multiple inheritance in C#

2019-02-17 06:05发布

问题:

As I am working as a C# developer, I know that we can implement multiple inheritance by use of Interface.

Can anybody please provide me link OR code for how to achieve multiple inheritance with C#.

I want code for how to achieve multiple inheritance in C# with the use of Interface.

Thanks in advance.

回答1:

Here is a good example.

http://blog.vuscode.com/malovicn/archive/2006/10/20/How-to-do-multiple-inheritance-in-C_2300_-2D00-Implementation-over-delegation-_2800_IOD_2900_.aspx

A quick code preview:

interface ICustomerCollection
{
      void Add(string customerName);
      void Delete(string customerName);
}

class CustomerCollection : ICustomerCollection
{
      public void Add(string customerName)
      {
            /*Customer collection add method specific code*/
      }
      public void Delete(string customerName)
      {
            /*Customer collection delete method specific code*/
      }
}

class MyUserControl: UserControl, ICustomerCollection
{
      CustomerCollection _customerCollection=new CustomerCollection();

      public void Add(string customerName)
      {
            _customerCollection.Add(customerName);
      }
      public void Delete(string customerName)
      {
            _customerCollection.Add(customerName);
      }
}


回答2:

It's not strictly multiple inheritance - but your C# class can implement multiple interfaces, that's right:

public class MyClass : BaseClass, IInterface1, IInterface2, IInterface3
{
   // implement methods and properties for IInterface1
   ..

   // implement methods and properties for IInterface2
   ..

   // implement methods and properties for IInterface3
   ..
}

You need to provide implementations for all methods (and properties) defined in all of the interfaces you're planning to implement.

I'm not quite clear on what you are looking for from your question.... can you clarify a bit?? What are you trying to do? Where are you running into trouble??



回答3:

Implementing multiple interfaces is NOT a replacement of multiple inheritance. Interfaces are there to specify the contract a class will adhere to. More like abstract classes.

If you want to achieve the effects of multiple inheritance, implementing Composite Pattern can help you.



回答4:

class MyClass : IFirstInterface, ISecondInterface, IThirdInterface
{
  // implement the methods of the interfaces
}


回答5:

Then there is this to inherit from more than one interface:

class EmptyClass : IDoSomething, IDoSomethingElse
{
}
interface IDoSomething
{
}
interface IDoSomethingElse
{
}

static class InterfaceExtensions
{
    public static int DoSomething(this IDoSomething tThis)
    {
        return 8;
    }
    public static int DoSomethingElse(this IDoSomethingElse tThis)
    {
        return 4;
    }
}

Using extension methods on the interface you can have something more like multiple inheritance than just tacking the interfaces on. Since the Methods are not part of the interface definition, you also don't have to implement them in the class, unless you want to. (Not really overriding them, you need a reference of type EmptyClass to call them since more specific, or exact type name, wins over inherited types.