ECMA-334 C# Language Specification17.10.1: Constructor initializers |
All instance constructors (except those for class object) implicitly include an invocation of another instance constructor immediately before the constructor-body
. The constructor to implicitly invoke is determined by the constructor-initializer:
argument-list
and the overload resolution rules of 14.4.2. The set of candidate instance constructors consists of all accessible instance constructors declared in the direct base class. If this set is empty, or if a single best instance constructor cannot be identified, a compile-time error occurs. argument-list
and the overload resolution rules of 14.4.2. The set of candidate instance constructors consists of all accessible instance constructors declared in the class itself. If that set is empty, or if a single best instance constructor cannot be identified, a compile-time error occurs. If an instance constructor declaration includes a constructor initializer that invokes the constructor itself, a compile-time error occurs. If an instance constructor has no constructor initializer, a constructor initializer of the form base() is implicitly provided.
is exactly equivalent to
C(...) {...}
end note]
C(...): base() {...}
The scope of the parameters given by the formal-parameter-list
of an instance constructor declaration includes the constructor initializer of that declaration. Thus, a constructor initializer is permitted to access the parameters of the constructor.
end example]
class A
{
public A(int x, int y) {}
}
class B: A
{
public B(int x, int y): base(x + y, x - y) {}
}
An instance constructor initializer cannot access the instance being created. Therefore it is a compile-time error to reference this in an argument expression of the constructor initializer, as it is a compile-time error for an argument expression to reference any instance member through a simple-name
.