Click here to Skip to main content
11,484,402 members (63,689 online)
Rate this: bad
good
Please Sign up or sign in to vote.
Hi
I want to understand the concept of multiple interfaces in C#.Can you please provide an example which elaborates this properly.
Also can somebody tell me why multiple inheritance is not possible in C#.
Posted 8-Jun-12 0:47am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Interfaces in C#.

Interface is a contract that defines the signature of the functionality.

So if a class is implementing a interface it says to the outer world, that it provides specific behavior.

Example if a class is implementing ‘Idisposable’ interface that means it has a functionality to release unmanaged resources.
Now external objects using this class know that it has contract by which it can dispose unused unmanaged objects.
  • Single Class can implement multiple interfaces.
  • If a class implements a interface then it has to provide
    implementation to all its methods.
Ref: an interface[^]

For more details: http://msdn.microsoft.com/en-us/library/ms173156.aspx[^]

why multiple inheritance is not possible in C#

The short answer is: because the language designers decided not to.
Multiple inheritance causes problems, if one or more of the parent classes defines members with same identifier.
You would have to decide which parent to use to handle each method call, and the operation of class can be then very different than intended.
Ref:Multiple Inheritance not allowed in Java or C#[^]

There are several reasons haven't provided a baked-in, verifiable, CLS-compliant version of multiple implementation inheritance:

1. Different languages actually have different expectations for how MI works. For example, how conflicts are resolved and whether duplicate bases are merged or redundant. Before we can implement MI in the CLR, we have to do a survey of all the languages, figure out the common concepts, and decide how to express them in a language-neutral manner. We would also have to decide whether MI belongs in the CLS and what this would mean for languages that don't want this concept (presumably VB.NET, for example). Of course, that's the business we are in as a common language runtime, but we haven't got around to doing it for MI yet.

2. The number of places where MI is truly appropriate is actually quite small. In many cases, multiple interface inheritance can get the job done instead. In other cases, you may be able to use encapsulation and delegation. If we were to add a slightly different construct, like mixins, would that actually be more powerful?

3. Multiple implementation inheritance injects a lot of complexity into the implementation. This complexity impacts casting, layout, dispatch, field access, serialization, identity comparisons, verifiability, reflection, generics, and probably lots of other places.
Ref: http://blogs.msdn.com/b/csharpfaq/archive/2004/03/07/85562.aspx[^]
  Permalink  
v2
Comments
VJ Reddy at 8-Jun-12 6:05am
   
Nicely explained. 5!
codeBegin at 8-Jun-12 6:09am
   
thanks VJ
shek124 at 8-Jun-12 6:09am
   
My 5!
codeBegin at 8-Jun-12 6:10am
   
thanks
Naveenaka Neo at 8-Jun-12 7:56am
   
Its a good explanation. But i am looking for an example where somebody implements multiple interfaces.
Naveenaka Neo at 12-Jun-12 8:39am
   
Thanks a Lot codeBegin, VJ reddy and shek124. This explanation helps me a lot in implementing the example.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I tried implementing multiple interfaces. This is a small example which describes the usage

code for vehicle.cs file
namespace MultipleInterface
{
  public interface TwoWheeler  { string vehicleType(); }
  public interface FourWheeler { string vehicleType(); }
   
  public class Vehicle: TwoWheeler, FourWheeler
  {
    public int vehicleNumber { get; set; }
    public string vehicleName { get; set; }
 
    string TwoWheeler.vehicleType()
    {
      return "2 wheeler type";
    }
    string FourWheeler.vehicleType()
    {
      return "4 wheeler type";
    }
  }
}

code for using these interfaces on default.aspx.cs file
  public partial class _Default : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
      Vehicle v = new Vehicle();
      TwoWheeler tv = new Vehicle();   // can only call function declared in TwoWheeler interface
      FourWheeler fv = new Vehicle();  // can only call function declared in FourWheeler interface
      v.vehicleName = "Nano";
      v.vehicleNumber = 123498;
      string output = string.Empty;
      output = "Name: " + v.vehicleName + "
Type2:" + tv.vehicleType() + "
Type:" + fv.vehicleType();
      Label1.Text = output;
    }
  }
code for default.aspx file
    <p>
        <asp:button id="Button1" runat="server" text="Run..." width="128px" xmlns:asp="#unknown">
            onclick="Button1_Click" />
    </asp:button></p>
    <asp:label id="Label1" runat="server" text="Label" xmlns:asp="#unknown"></asp:label>

Hope it will help beginners. Thanks a lot.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 190
1 Sergey Alexandrovich Kryukov 170
2 Raje_ 155
3 OriginalGriff 147
4 Mehdi Gholam 120
0 Sergey Alexandrovich Kryukov 8,484
1 OriginalGriff 7,948
2 Sascha Lefèvre 3,199
3 Maciej Los 2,681
4 Richard Deeming 2,370


Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 12 Jun 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100