Using Multiple Interfaces

Environment: .NET / C#

One of the benefits of implementing interfaces instead of inheriting from a class is that you can implement more than one interface at a time. This gives you the power to do multiple inheritance without some of the downside.

To implement multiple interfaces in C#, you separate each included interface with a comma. For example, to include both an Ishape and an IshapeDisplay interface in a Square class, you use the following:

class Square : IShape, IShapeDisplay

You then need to implement all the constructs within both interfaces. Listing 1 illustrates the use of multiple interfaces.

Listing 1 - Multi.cs - Implementing Multiple Interfaces in a Single Class

 1:  //  Multi.cs - 
 2:  //-----------------------------------------------------------
 4:  using System;
 6:  public interface IShape
 7: {
 8:     // Cut out other methods to simplify example.
 9:     double Area();
10:     int Sides { get; }
11:  }
13:  public interface IShapeDisplay
14:  {
15:     void Display();
16:  }
18:  public class Square : IShape, IShapeDisplay
19:  {
20:     private int InSides;
21:     public  int SideLength;
23:     public int Sides 
24:     {
25:        get { return InSides; }
26:     }
28:     public double Area()
29:     {
30:        return ((double) (SideLength * SideLength));
31:     }
33:     public double Circumference()
34:     {
35:        return ((double) (Sides * SideLength));
36:     }
38:     public Square()
39:     {
40:        InSides = 4;
41:     }
43:     public void Display()
44:     {
45:        Console.WriteLine("\nDisplaying Square information:");
46:        Console.WriteLine("Side length: {0}", this.SideLength);
47:        Console.WriteLine("Sides: {0}", this.Sides);
48:        Console.WriteLine("Area: {0}", this.Area());
49:     }
50:  }
52:  public class Multi
53:  {
54:     public static void Main()
55:     {
56:        Square mySquare = new Square();
57:        mySquare.SideLength = 7;
59:        mySquare.Display();
60:     }
61:  }


Displaying Square information:
Side length: 7
Sides: 4
Area: 49

You can see that two interfaces are declared and used in this listing. In Line 18, you can see that the Square class will implement the two interfaces. Because both are included, all members of both interfaces must be implemented by the Square class. In looking at the code in Lines 23 - 49, you see that all the members are implemented. You can implement multiple interfaces in your own applications in the same way.


Download source - - 1 Kb

About the Author

Bradley Jones

Bradley Jones, in addition to managing CodeGuru, Brad! oversees the Newtwork of sites including Codeguru,, DevX, VBForums, and over a dozen more with a focus on software development and database technologies. His experience includes development in C, C++, VB, some Java, C#, ASP, COBOL, and more as well as having been a developer, consultant, analyst, lead, and much more. His recent books include Teach Yourself the C# Language in 21 Days, Web 2.0 Heroes, and Windows Live Essentials and Services.
Google+ Profile | Linked-In Profile | Facebook Page


  • Comment

    Posted by iyanar.k on 04/23/2013 03:48am

    Nice and simple to understand. Keep posting

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Complex hybrid environments can make it difficult to track interdependencies, increasing the risk of disrupting critical business services. In this white paper by EMA, you'll learn how application discovery and dependency mapping can help you: Meet granular targets for availability, cost, and time-to-revenue for cloud services. Accelerate mean time to repair (MTTR) while communicating better with stakeholders. Manage even the most complex hybrid environments more efficiently and effectively Understand the …

  • More than ever, agile Software Life Cycle Development and DevOps drives critical top-line business impact for customers across a broad range of industries. Learn how XtremIO is fundamentally enabling the next generation of agile Software Development & DevOps to: Radically improve developer efficiency and overall product quality via an All-Flash Dev Ops platform Enable modern lightning fast dev methodologies with zero-cost application & datasets full copies. Allow Developers to have zero storage obstacles, …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date