Interface in c# with example

As everyone aware with Diamond Problem (Multiple inheritance) in C#. For the deeper look, the "diamond problem" (sometimes referred to as the "deadly diamond of death) is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If there is a method in A that B and C have overridden, and D does not override it, then which version of the method does D inherit: that of B, or that of C. So achieving this multiple inheritance we try to use interface in our Project/Solution. Mean to say, we cannot inherit from multiple classes, but you can implement multiple interfaces.

Diamond Problem in oops

                                                                        Diamond Problem in C#

An interface looks like an abstract class. Why, Interface it look like an abstract class because It has only declaration similar to Abstract class (Abstract methods), there is no implementation for them within the interface. The class who implements that interface is responsible for defining those methods. An interface only contains the signatures of declarations of events, indexers, methods and properties. All of these are public in Interface, There is no need to write a public for methods of the interface. Because the interface assumes everything in the interface is public.

In this article, I try to explain two important uses of the interface.

1. For Multiple inheritance.

2. Expose useful information to others.

Here I am giving an example to explain uses of the interfaces.

Suppose there is an Organization have no of task, roles and team, every team have own task and responsibility. But we need to stop that one team can interfere or access at task of another team. To achieve this situation we need to use Interface.

namespace OrgModel
    interface IUiTeam
        void Desiging();
        void Testing();
    interface IDevlopmentTeam
        void Coding();
        void Testing();
    interface IMarktingTeam
         void Sales();
        void Marketing();

    class Organization : IUiTeam, IDevlopmentTeam, IMarktingTeam   //Multiple inheritance
        public void Desiging()
            Console.WriteLine("Design UI");
        public void Coding()
            Console.WriteLine("Doing Code");
        public void Testing()
            Console.WriteLine("Test Project Bug");
        public void Sales()
            Console.WriteLine("Sale work");
        public void Marketing()
            Console.WriteLine("Marking tasks");

    class Program
        static void Main(string[] args)
            IUiTeam UiTeam = new Organization(); // Team Ui & responsibility

            IDevlopmentTeam DevTeam = new Organization(); //Team Development & responsibility

            IMarktingTeam MarketingTeam = new Organization(); //Team Marketing & responsibility



As per code above Organization (Class Organization), Have few operations to i.e. Designing (), Coding (), Testing (), Sales (), Marketing (). Here we create three interface (IUiTeam, IDevlopmentTeam, IMarktingTeam) as team assign them their responsibility, like IUiTeam have only method Designing () and Testing (). Now while we create object of Organization using IUiTeam interface, It expose only method which are assign. Have a look at image below.

Here we just restrict this IUiTeam team to use only these two methods. It unable to access other method of Organization (Class Organization). So we do the same with other interface (IDevlopmentTeam, IMarktingTeam) assign them their own responsibility.






share post :

Popular Posts

How to Create WCF service and host in Windows Service in WCF Service / Jan 13, 2017
Dependency properties in WPF in WPF / Oct 01, 2018
Static constructor in CSharp(C#.Net) in C#.Net / Feb 11, 2017
How to use tuples with more new feature in C# 7.0 in C#.Net / Jan 20, 2017
Method Overloading in C# in C#.Net / Feb 07, 2017
Interface in c# with example in C#.Net / May 14, 2017

Tag Cloud

Say Hi It’s Free!

  • Ghaziabad Uttar Pradesh India


  • (+91)-847-107-5432

Success/Error Message Goes Here

Avnish Kumar

.Net Developer

Download Resume
Drink A Coffee With Me Today