为什么 C# 编译器显式声明类型实现的所有接口?
C# 编译器似乎明确记录了它及其基类实现的所有接口。 CLI 规范表明这不是必需的。 我见过一些其他编译器没有显式地发出这个,而且它似乎工作得很好。 C# 这样做有什么区别或原因吗?
底部的 C# 为 B 生成的 MSIL 是:
.class private auto ansi beforefieldinit B
extends A
implements IAdvanced,
ISimple
它不需要指定 ISimple,因为 A 与 IAdvanced 一样实现它。 C#代码:
interface ISimple {
int Basic { get; }
int Zero { get; }
}
interface IAdvanced : ISimple {
string Major { get; }
}
class A : ISimple {
int ISimple.Basic {
get { return 1; }
}
int ISimple.Zero {
get{ return 0;}
}
}
class B : A, IAdvanced {
string IAdvanced.Major {
get { return "B"; }
}
}
The C# compiler seems to explicitly note all interfaces it, and its base classes implement. The CLI specs say that this is not necesary. I've seen some other compilers not emit this explicitly, and it seems to work fine. Is there any difference or reason the C# does this?
The MSIL that the C# at the bottom generates for B is:
.class private auto ansi beforefieldinit B
extends A
implements IAdvanced,
ISimple
It shouldn't need to specify ISimple, because A implements it as does IAdvanced. C# code:
interface ISimple {
int Basic { get; }
int Zero { get; }
}
interface IAdvanced : ISimple {
string Major { get; }
}
class A : ISimple {
int ISimple.Basic {
get { return 1; }
}
int ISimple.Zero {
get{ return 0;}
}
}
class B : A, IAdvanced {
string IAdvanced.Major {
get { return "B"; }
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为我们不能在这里知道任何明确的答案,除非我们让编译器开发人员介入。但是,我们可以猜测原因。 它可能是:
I don't think we can know any definitive answer here, unless we have the compiler developers drop in. However, we can guess about the reasons. It could be: