Here is an example of code:
public List(int capacity = defaultCapacity) {
items = new T[capacity];
}
In C# 5 Language Specification Section 1.6.7
is written:
Instance constructors can be overloaded. For example, the List class declares two instance constructors, one with no parameters and one that takes an int parameter.
But compiled IL
for this code doesn't contain 2 constructors. It contains only this declaration:
.method public hidebysig specialname rtspecialname
instance void .ctor([opt] int32 capacity) cil managed
It means that optional parameter is CLR
level and is defined by [opt]
.
After CLR
there is no runtime that can represent this object with 2 overloaded constructors.
For Example if I am creating 2 separate constructors without optional parameter compiled IL
will contain 2 .ctor
-s.
I want to clarify, if language specification is saying that class declares two instance constructors
doesn't it mean that compiled IL
will contain 2 ctor
-s too.