All constructors (except for the constructors of class object
) implicitly include an invocation of another constructor immediately before the first statement in the block of the constructor. The constructor to implicitly invoke is determined by the constructor-initializer:
base(...)
causes a constructor from the direct base class to be invoked. The constructor is selected using the overload resolution rules of §7.4.2. The set of candidate constructors consists of all accessible constructors declared in the direct base class. If the set of candidate constructors is empty, or if a single best constructor cannot be identified, an error occurs.this(...)
causes a constructor from the class itself to be invoked. The constructor is selected using the overload resolution rules of §7.4.2. The set of candidate constructors consists of all accessible constructors declared in the class itself. If the set of candidate constructors is empty, or if a single best constructor cannot be identified, an error occurs. If a constructor declaration includes a constructor initializer that invokes the constructor itself, an error occurs.If a constructor has no constructor initializer, a constructor initializer of the form base()
is implicitly provided. Thus, a constructor declaration of the form
C(...) {...}
is exactly equivalent to
C(...): base() {...}
The scope of the parameters given by the formal-parameter-list of a constructor declaration includes the constructor initializer of that declaration. Thus, a constructor initializer is permitted to access the parameters of the constructor. For example:
class A { public A(int x, int y) {} } class B: A { public B(int x, int y): base(x + y, x - y) {} }
A constructor initializer cannot access the instance being created. It is therefore an error to reference this
in an argument expression of the constructor initializer, as is it an error for an argument expression to reference any instance member through a simple-name.