Suppose class is implementing two interfaces and both the interfaces have function name Todo() with same signature. How to implement these functions in class?
tarun kumar
To implement functions with the same signature from multiple interfaces in a class, you can use explicit interface implementation. By prefixing the interface name before the method, you can disambiguate between the two methods.
using System;interface IInterface1{ void Todo();}interface IInterface2{ void Todo();}class MyClass : IInterface1, IInterface2{ void IInterface1.Todo() { Console.WriteLine("Implementation of Todo from IInterface1"); } void IInterface2.Todo() { Console.WriteLine("Implementation of Todo from IInterface2"); }}class Program{ static void Main() { MyClass obj = new MyClass(); ((IInterface1)obj).Todo(); ((IInterface2)obj).Todo(); }}
using System;
interface IInterface1
{
void Todo();
}
interface IInterface2
class MyClass : IInterface1, IInterface2
void IInterface1.Todo()
Console.WriteLine("Implementation of Todo from IInterface1");
void IInterface2.Todo()
Console.WriteLine("Implementation of Todo from IInterface2");
class Program
static void Main()
MyClass obj = new MyClass();
((IInterface1)obj).Todo();
((IInterface2)obj).Todo();
MyClass implements both IInterface1 and IInterface2 with separate implementations of the Todo method for each interface. By casting the object to the respective interfaces, you can call the specific implementations.