Wednesday, 22 January 2020

How to implement two interface with same method in C#?

Hi All,

Hope you all are doing good.

In this article, I will explain you about how to implement two interfaces with same method in C#.


Answer: If we have two interface with same method name then a class need to implement interface explicitly in a program.

For more information, please go through below snippet of code.

namespace InterfaceExample
{
    class Program
    {
        static void Main(string[] args)
        {
            IInterface1 objI1 = new Test();
            objI1.Print();

            IInterface2 objI2 = new Test();
            objI2.Print();

            Console.ReadKey();
        }
    }

    interface IInterface1
    {
        void Print();
    }

    interface IInterface2
    {
        void Print();
    }

    class Test : IInterface1, IInterface2
    {
        void IInterface1.Print()
        {
            Console.WriteLine("Print text of IInterface1");
        }
        
        void IInterface2.Print()
        {
            Console.WriteLine("Print text of IInterface2");
        }
    }
}

Thanks for reading my article.

No comments:

Post a Comment

Intoduction to Flutter

Hi All, I hope every one is doing good In this article, we will know about the introduction of Flutter.