这是一个代码示例:
public List(int capacity = defaultCapacity) {
items = new T[capacity];
}
中C# 5 Language Specification Section 1.6.7写着:
实例构造函数可以重载。例如,List 类声明了两个实例构造函数,一个没有参数,一个接受 int 参数。
但是IL为此代码编译的不包含 2 个构造函数。它只包含这个声明:
.method public hidebysig specialname rtspecialname
instance void .ctor([opt] int32 capacity) cil managed
这意味着可选参数是CLR级别的,由[opt].
在CLR没有运行时可以用 2 个重载的构造函数表示这个对象之后。
例如,如果我创建 2 个没有可选参数的单独构造函数,编译IL将包含 2 个.ctor-s。
我想澄清一下,如果语言规范说这class declares two instance constructors并不意味着编译后IL也将包含 2 ctor-s。