Interface a and b having same method, how you called the method in C#
In C#, if two interfaces `A` and `B` have a method with the same signature, and a class implements both interfaces, the class must provide an implementation of the common method. Here's an example demonstrating this scenario:
```csharp
using System;
// Interface A
interface A
{
void CommonMethod();
}
// Interface B
interface B
{
void CommonMethod();
}
// Class implementing both interfaces
class MyClass : A, B
{
// Explicit implementation of the CommonMethod from interface A
void A.CommonMethod()
{
Console.WriteLine("Implementation of CommonMethod from interface A");
}
// Explicit implementation of the CommonMethod from interface B
void B.CommonMethod()
{
Console.WriteLine("Implementation of CommonMethod from interface B");
}
}
class Program
{
static void Main(string[] args)
{
MyClass myClass = new MyClass();
// Calling the CommonMethod through interface A
((A)myClass).CommonMethod();
// Calling the CommonMethod through interface B
((B)myClass).CommonMethod();
Console.ReadKey();
}
}
```
In the above example, the `MyClass` class implements both interfaces `A` and `B`. To differentiate between the implementations of the `CommonMethod` from both interfaces, you can use explicit interface implementation syntax.
When you call the `CommonMethod` through interface `A`, you need to cast the object to interface `A`, and similarly, when you call it through interface `B`, you cast the object to interface `B`. This way, you can provide separate implementations for the same method signature in different interfaces.