?? Tech Tip: Resolving Method Name Ambiguity in C#

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

要查看或添加评论,请登录

Shahin Al Kabir (Mitul)的更多文章

社区洞察

其他会员也浏览了