X hits on this document





265 / 396


C(...) {...}

is exactly equivalent to

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. For 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 is it a compile-time error for an argument expression to reference any instance member through a simple-name.

10.10.2 Instance variable initializers

When an instance constructor has no constructor initializer, or it has a constructor initializer of the form base(...), that constructor implicitly performs the initializations specified by the variable-initializers of the instance fields declared in its class. This corresponds to a sequence of assignments that are executed immediately upon entry to the constructor and before the implicit invocation of the direct base class constructor. The variable initializers are executed in the textual order in which they appear in the class declaration.

10.10.3 Constructor execution

Variable initializers are transformed into assignment statements, and these assignment statements are executed before the invocation of the base class instance constructor. This ordering ensures that all instance fields are initialized by their variable initializers before any statements that have access to that instance are executed.

Given the example

using System;

class A { public A() { PrintFields(); }

public virtual void PrintFields() {}


class B: A { int x = 1; int y;

public B() { y = -1; }

252Copyright Microsoft Corporation 1999-2003. All Rights Reserved.

Document info
Document views1259
Page views1259
Page last viewedFri Jan 20 09:52:46 UTC 2017