Question

Take this example:

public interface IFoo
{
    IFoo Bar();
}

public class Foo : IFoo
{
    public Foo Bar()
    {
        //...
    }

    IFoo IFoo.Bar() { return Bar(); } //Why is this necessary?
}

Why is the implicit implementation of IFoo Bar() necessary even though Foo converts to IFoo without a cast?

Was it helpful?

Solution 3

You can solve it like this (a bit ugly, but takes care of the strong typing):

public interface IFoo<T> where T : IFoo<T>
{
    T Bar();
}

public class Foo : IFoo<Foo>
{
    public Foo Bar()
    {
        //...
    }
}

OTHER TIPS

It's needed in this case because C# does not support return type co-variance for interfaces, so your function

public Foo Bar()
{
    //...
}

does not satisfy the IFoo interface since the return type of the Bar method is different.

Since you want to also implement the interface, your only choice is to do so explicitly since you already have a Bar() method defined on the class.

Microsoft has a detailed write up on the subject but it boils down to the implementation of multiple interfaces/classes which have the same method in them. Implicit no longer works in that context.

class Test 
{
    static void Main()
    {
        SampleClass sc = new SampleClass();
        IControl ctrl = (IControl)sc;
        ISurface srfc = (ISurface)sc;

        // The following lines all call the same method.
        sc.Paint();
        ctrl.Paint();
        srfc.Paint();
    }
}


interface IControl
{
    void Paint();
}
interface ISurface
{
    void Paint();
}
class SampleClass : IControl, ISurface
{
    // Both ISurface.Paint and IControl.Paint call this method.  
    public void Paint()
    {
        Console.WriteLine("Paint method in SampleClass");
    }
}

// Output: 
// Paint method in SampleClass 
// Paint method in SampleClass 
// Paint method in SampleClass

If we were to take the explicit approach, we end up with this.

public class SampleClass : IControl, ISurface
{
    void IControl.Paint()
    {
        System.Console.WriteLine("IControl.Paint");
    }
    void ISurface.Paint()
    {
        System.Console.WriteLine("ISurface.Paint");
    }
}

It all boils down to providing uniqueness when the implemented types clash. In your example, Foo is IFoo.

Because you may not always want the method implementing the interface to behave the same way as another version of the method with the same signature.

You may also want a class to implement a method for an interface but for that method not be be accessible from an instance of the class itself.

I'd recommend that you kept the implicit implementation as protected instead of public.

public class Foo : IFoo
{
    **protected virtual** Foo Bar()
    {
        //...
    }

    IFoo IFoo.Bar() { return Bar(); } 
}

there is a pretty extensive answer for why/when to use explicit implementation in this thread:

implicit vs explicit interface implementation

A good reason for using the explicit implementation is that you can easily use dependency injection to have a more loose coupling when you use your Foo class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top