如果一个接口指定了一个属性或方法来返回另一个接口,为什么不允许第一个接口的实现将返回类型“更改”为更具体的类型?
我们举个例子来说明:
interface IFoo
{
    IBar GetBar();
}
interface IBar
{ }
class Foo : IFoo
{
    // This is illegal, we are not implementing IFoo properly
    public Bar GetBar()
    {
        return new Bar();
    }
}
class Bar : IBar
{ }
我知道如何让它发挥作用,这不是我关心的。
我可以:
- 将返回类型更改GetFoo()为IBar, 或
- 显式实现接口并GetBar从IFoo.GetBar()方法中调用
我真正要问的是不仅仅是允许上面的代码编译的原因。是否存在上述不履行规定的合同的情况IFoo。