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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With