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 posts from this blog

CAML Query in SharePoint 2010

Calling REST APi from server side code - C#

All about SharePoint 2010 Content Type Hub

SharePoint Interview Questions and Answers

Calling ASP.Net WebMethod using jQuery AJAX