Inherit Multiple Interfaces With the Same Method Name in C#

In this article we will see a situation that occurs when two interfaces have methods with the same name and the same parameter(s) and one base class implements these interfaces. For example, we create interfaces that have methods with the same name. After that we create a class that inherits from multiple interfaces.

What is Interface?

Interfaces in C# are provided as a replacement of multiple inheritance. Interface contains all abstract methods inherited by classes and structs, which must provide an implementation for each interface member declared. The keyword interface creates an interface. They are public by default.

Creating  Interface with the Same Method Name

Now we create interfaces named A and B. The A and B interfaces contain functions with the same name, Hello().

interface A

{

    void Hello();

}

 

interface B

{

    void Hello();

}

 

Now we will see how to implement these methods in my class.

 

using System;

 

interface A

{

    void Hello();

}

 

interface B

{

    void Hello();

}

 

class Test : A, B   

{

    public void Hello()

    {

        Console.WriteLine("Hello to all");

    }

}

 

public class interfacetest

{

    public static void Main()

    {

        Test Obj1 = new Test();

        Obj1.Hello();

    }

 

Now Press F5 to execute the preceding program.

 

Interface-1.jpg

 

The preceding output creates more confusion about which method of which interface is implemented. All the methods defined in an interface should be implemented.

 

We need to tell the compiler which class function we want to implement. For such cases, we need to use the name of the interface during the implementation of a function. Have a look at the following example:

 

using System;

 

interface A

{

    void Hello();

}

 

interface B

{

    void Hello();

}

 

class Test : A, B

{

    void A.Hello()

    {

        Console.WriteLine("Hello to all-A");

    }

}

 

public class interfacetest

{

    public static void Main()

    {

        A Obj1 = new Test();

        Obj1.Hello();

    }

}

 

In the preceding example to tell compiler which method of which interface is implemented in class "Test" you can use the name of the interface during the function's implementation. Now press F5 to execute the preceding program. It will show an error.

Interface-error-2.jpg

Now we implement the method in the Test class for interface B.

using System;

 

interface A

{

    void Hello();

}

 

interface B

{

    void Hello();

}

 

class Test : A, B

{

    void A.Hello()

    {

        Console.WriteLine("Hello to all-A");

    }

 

    void B.Hello()

    {

        Console.WriteLine("Hello to all-B");

    }

}

 

public class interfacetest

{

    public static void Main()

    {

        A Obj1 = new Test();

        Obj1.Hello();

 

        B Obj2 = new Test();

        Obj2.Hello();

    }

}

 

Note: So in your example, you are calling the "Hello" method on the "Test" class. However, Test implements both Hello functions. Therefore, it can be treated as both for one object. So you need to assign an object of that class to a variable defined as that specific interface.

 

Now press F5 to execute it.

 

Interface-3.jpg

 

Up Next
    Ebook Download
    View all
    Learn
    View all