Can we implement interface with same method name?

Interface is similar to a class, and we implement interface by defining their method in class. Suppose we have same method in more than one interface.

In this case there will be a question for interview.

Q.    Can we implement interface with same method name?
Ans. Yes by using explicit interface implementation.

Lets see below example:

  class Program
    {
        static void Main(string[] args)
        {
            MyInterface1 obj1 = new MyClass();
            MyInterface2 obj2 = new MyClass();

            obj1.func1();
            obj2.func1();
        }
        interface MyInterface1
        {
            void func1();
        }

        interface MyInterface2
        {
            void func1();
        }

        class MyClass : MyInterface1, MyInterface2
        {
            void  MyInterface1.func1()
            {
                Console.WriteLine("Hello1");
            }

            void MyInterface2.func1()
            {
                Console.WriteLine("Hello2");
            }
        }
    } 

Comments

Popular

Top 8 C# 6.0 Features

SharePoint Interview Questions and Answers

Calling ASP.Net WebMethod using jQuery AJAX

What is Cloud Computing - A guide for beginners