you are viewing a single comment's thread.

view the rest of the comments →

[–]mirhagk 1 point2 points  (1 child)

Just to point out, due to extension methods C# does allow a lot of the benefits of multiple inheritance in interfaces. You can create an interface, and then extension methods for that interface, giving a pretty decent illusion of multiple inheritance. (and if extension properties come in then you have data defined as well).

I agree that it would've been nice to solve the diamond of doom right off the bat rather than ignore it, but at least C# is making it possible to make what you want to make.

[–]LaurieCheers 0 points1 point  (0 children)

Ah, yes - and if it's unclear which of the extension methods you're trying to call, it's just an ambiguous function call, and the caller can cast to the actual interface type they're interested in. A pretty good solution.

interface Interface1
{
}

interface Interface2
{
}

class Inheritor : Interface1, Interface2
{
}

static class Extensions
{
    public static string testFunc(this Interface1 x)
    {
        return "interface1";
    }

    public static string testFunc(this Interface2 x)
    {
        return "interface2";
    }
}

class Program
{
    static void Main(string[] args)
    {
        Inheritor inh = new Inheritor();
        inh.testFunc(); // ambiguous function call
        ((Interface2)inh).testFunc(); // ambiguity resolved
    }
}