views:

217

answers:

5

Hi All,

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.

+11  A: 

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??

marc_s
Marc,I want to know that How would we can get exact behavior in multiple inheritance with Interface just like we inherit single class. Would also like to know that can we cast our current object with another class with the use of Interface? (May be this is called MH)
nunu
you **cannot** get the exact same behavior with implementing interfaces as you would with true multiple inheritance. Interfaces only define a functional interface - you cannot inherit from multiple implementations. It's an approximation - not an exact substitute.
marc_s
@marc_s: "strict multiple inheritance" does not exist, however some consensus seems to be that "strict" means multiple implementation inheritance. As it comes, *multiple interface inheritance* is just another form of multiple inheritance, of which many forms exist (Meyer describes 16 such forms p824+ in OOSC).
Abel
+1  A: 
class MyClass : IFirstInterface, ISecondInterface, IThirdInterface
{
  // implement the methods of the interfaces
}
Mathias
+6  A: 

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);
      }
}
samer
+4  A: 

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.

Hemant
+1  A: 

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.

Rangoric