Click here to Skip to main content
12,547,806 members (44,772 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# ASP.NET Inheritance
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 7-Jun-12 23:47pm
Rate this: bad
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:[^]

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
Naveenaka Neo 8-Jun-12 7:56am
Its a good explanation. But i am looking for an example where somebody implements multiple interfaces.
Naveenaka Neo 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
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
    <asp:button id="Button1" runat="server" text="Run..." width="128px" xmlns:asp="#unknown">
        onclick="Button1_Click" />

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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.161021.1 | Last Updated 12 Jun 2012
Copyright © CodeProject, 1999-2016
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