?? Tech Tip: Resolving Method Name Ambiguity in C#
Shahin Al Kabir (Mitul)
Lead Software Engineer | fintech solutions | Digital Lending | Manager @ BRAC Bank PLC
Ever faced a method name clash while inheriting from two interfaces in C#? Here's a quick solution using interface implementation:
interface IFirstInterface { void DoSomething(); }
interface ISecondInterface { void DoSomething(); }
class MyClass : IFirstInterface, ISecondInterface
{
void IFirstInterface.DoSomething() => Console.WriteLine("First interface action");
void ISecondInterface.DoSomething() => Console.WriteLine("Second interface action");
}
// Usage:
var myObject = new MyClass();
((IFirstInterface)myObject).DoSomething();
((ISecondInterface)myObject).DoSomething();
By explicitly casting the object to the respective interface, you tackle method ambiguity gracefully. ???? #CSharp #Programming #TechTips