110,533
社区成员
发帖
与我相关
我的任务
分享
public interface IList : ICollection, IEnumerable
public interface ICollection : IEnumerable
public interface IList : ICollection
public interface IList : ICollection
这样的了,因为微软写 .net
新的类库的时候就会使用 c# 最新版本来写代码。 class Program
{
static void Main(string[] args)
{
IA x = new XX();
}
}
interface IA
{
string X { get; }
}
interface IB : IA
{
string Y { set; }
}
interface IAB: IB
{
}
public class XX : IAB
{
public string Y { set => throw new NotImplementedException(); }
public string X => throw new NotImplementedException();
}
它编译到 MSIL 之后,编译器产生的代码是.class public auto ansi beforefieldinit ConsoleApp1.XX
extends [mscorlib]System.Object
implements ConsoleApp1.IAB,
ConsoleApp1.IB,
ConsoleApp1.IA
{
// Methods
.method public final hidebysig specialname newslot virtual
instance void set_Y (
string 'value'
) cil managed
{
// Method begins at RVA 0x206d
// Code size 6 (0x6)
.maxstack 8
IL_0000: newobj instance void [mscorlib]System.NotImplementedException::.ctor()
IL_0005: throw
} // end of method XX::set_Y
..............................
可以看出,其实 .net framework 根本不支持接口继承,所以编译器自动地在生成 XX 类型定义代码时及加入了更多的接口声明。
早期的 c# 并不支持接口继承,并不会产生这类代码。这是最近10年的c#编译器才有的特性。而最早的 c# 源代码,或者你只是通过“反编译工具”看到的源代码,就会看到public class XX : IAB, IB, IA
{
.............
这类代码形式。