Click here to Skip to main content
15,346,923 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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#.

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:[^]

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.
VJ Reddy 8-Jun-12 6:05am
Nicely explained. 5!
codeBegin 8-Jun-12 6:09am
thanks VJ
shek124 8-Jun-12 6:09am
My 5!
codeBegin 8-Jun-12 6:10am
NaveenNeo 8-Jun-12 7:56am
Its a good explanation. But i am looking for an example where somebody implements multiple interfaces.
NaveenNeo 12-Jun-12 8:39am
Thanks a Lot codeBegin, VJ reddy and shek124. This explanation helps me a lot in implementing the example.
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 + "<br />Type2:" + tv.vehicleType() + "<br />Type:" + fv.vehicleType();
    Label1.Text = output;

code for default.aspx file
    <asp:button id="Button1" runat="server" text="Run..." width="128px" xmlns:asp="#unknown">
        onclick="Button1_Click" />
<asp:label id="Label1" runat="server" text="Label" xmlns:asp="#unknown"></asp:label>

Hope it will help beginners. Thanks a lot.

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900