This Angular tutorial helps you get started with Angular quickly and effectively through many practical examples.

Multiple Interfaces-Same Method Name


In this article we are going to discuss multiple Interfaces with same Method Name in C#.

In C#, a class can implement multiple interfaces that have methods with the same name. When this happens, you should have different implementations for each interface, you can use explicit interface implementation.

Example

In this example, Test class provides separate implementations for A.Hello() and B.Hello(). When calling these methods, you need to cast the object to the appropriate interface type.


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();
    }
}

Output

Multiple Interface with same method name in C#
Prev Next