13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 17 Classes17.10.1 Constructor initializersAll instance constructors (except those for class object) implicitly include an invocation of another instanceconstructor immediately before the constructor-body. The constructor to implicitly invoke is determined by theconstructor-initializer:• An instance constructor initializer of the form base(argument-list opt ) causes an instance constructor fromthe direct base class to be invoked. That constructor is selected using argument-list and the overloadresolution rules of §14.4.2. The set of candidate instance constructors consists of all accessible instanceconstructors declared in the direct base class, or the default constructor (§17.10.4), if no instance constructorsare declared in the direct base class. If this set is empty, or if a single best instance constructor cannot beidentified, a compile-time error occurs.• An instance constructor initializer of the form this(argument-list opt ) causes an instance constructor fromthe class itself to be invoked. The constructor is selected using argument-list and the overload resolution rulesof §14.4.2. The set of candidate instance constructors consists of all accessible instance constructors declaredin the class itself. If that set is empty, or if a single best instance constructor cannot be identified, a compiletimeerror occurs. If an instance constructor declaration includes a constructor initializer that invokes theconstructor itself, a compile-time error occurs.If an instance constructor has no constructor initializer, a constructor initializer of the form base() is implicitlyprovided. [Note: Thus, an instance constructor declaration of the formC(…) {…}is exactly equivalent toC(…): base() {…}end note]The scope of the parameters given by the formal-parameter-list of an instance constructor declaration includesthe constructor initializer of that declaration. Thus, a constructor initializer is permitted to access the parametersof the constructor. [Example: For example:class A{public A(int x, int y) {}}class B: A{public B(int x, int y): base(x + y, x - y) {}}end example]An instance constructor initializer cannot access the instance being created. Therefore it is a compile-time error toreference this in an argument expression of the constructor initializer, as it is a compile-time error for anargument expression to reference any instance member through a simple-name.17.10.2 Instance variable initializersWhen an instance constructor has no constructor initializer, or it has a constructor initializer of the formbase(…), that constructor implicitly performs the initializations specified by the variable-initializers of theinstance fields declared in its class. This corresponds to a sequence of assignments that are executed immediatelyupon entry to the constructor and before the implicit invocation of the direct base class constructor. The variableinitializers are executed in the textual order in which they appear in the class declaration.17.10.3 Constructor executionVariable initializers are transformed into assignment statements, and these assignment statements are executedbefore the invocation of the base class instance constructor. This ordering ensures that all instance fields areinitialized by their variable initializers before any statements that have access to that instance are executed.[Example: For example:259

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!