实现基接口和派生接口

问题描述 投票:0回答:1
interface IBaseInterface
{
}

interface IDerivedInterface1 : IBaseInterface
{
}


class MyClass1 :
    IDerivedInterface1
{
}

class MyClass2 :
    IDerivedInterface1,
    IBaseInterface
{
}

MyClass2
MyClass1
有什么优点或缺点吗?

我们什么时候应该或不应该派生一个像

MyClass2
这样的类?

c# interface derived-class
1个回答
0
投票

来自语言规范

直接实现接口的类或结构也隐式实现该接口的所有基接口。即使类或结构没有显式列出基类列表中的所有基接口,也是如此。

因此,这里要做出的明显声明是,如果您想显式实现一个或多个接口,那么您需要在基类列表中命名这些接口。 否则,如果您只是使用隐式接口实现,那么无论您是否命名基本接口,编译的输出都应该相同。

© www.soinside.com 2019 - 2024. All rights reserved.