What will be the constructor flow of control

D

Dhananjaya.R

Hi,

What will be the constructor flow of control for the below snippet. I had an
hard time understanding how C# can allow initialization of member variables
in class declaration.

public class Base {
public int x = a( );
public Base( ) { b( ); }
}

public class D1: Base {
public int y = c( );
public D1( ) { d( ); }
}

public class D2 : D1 {
public int z = e( );
public D2( ) { f( ); }
}

public class D3 : D2 {
public int w = g( );
public D3( ) { h( ); }
}
 
J

Jon Skeet [C# MVP]

Dhananjaya.R said:
What will be the constructor flow of control for the below snippet. I had an
hard time understanding how C# can allow initialization of member variables
in class declaration.

public class Base {
public int x = a( );
public Base( ) { b( ); }
}

public class D1: Base {
public int y = c( );
public D1( ) { d( ); }
}

public class D2 : D1 {
public int z = e( );
public D2( ) { f( ); }
}

public class D3 : D2 {
public int w = g( );
public D3( ) { h( ); }
}

Member variables are initialized just *before* the base constructor is
called (unlike in Java) so the order goes:

g()
e()
c()
a()
b()
d()
f()
h()
 
J

Joel Martinez

well ... the code you supplied does not compile so I'm having a hard
time understanding what exactly you're trying to figure out. member
variables are intialized when the object is instantiated.

does that help?

Joel Martinez
http://www.onetug.org - Orlando .NET User Group
http://www.codecube.net - Blog
 
J

Jon Skeet [C# MVP]

Joel Martinez said:
well ... the code you supplied does not compile so I'm having a hard
time understanding what exactly you're trying to figure out. member
variables are intialized when the object is instantiated.

does that help?

I suspect the OP wants to know whether the base constructor is called
before or after the member variables are initialized; see my other
response for an answer.
 
D

Dhananjaya.R

Hi Jon,

Thanks for your information .. I had a confusion bcoz I am from VC++
background and so was thinking in that terms..

Is it only in the C# or all .Net languages support this functionality.
 
R

Richard Blewett [DevelopMentor]

This is the order in which the C# compiler emits the instructions into the constructor in IL

Field initializers simply become statements in the constructor once compiled - the CLR has no notion of what they are. The C# compiler emits them into the constructor before it puts the call to the base constructor in, then it puts the derived constructor body.

It does this because if you call a virtual method from a base class constructor and that virtual method has been overridden in the derived class, the derived version will fire. Since the derived constructor body hasn't yet been run, running the field initializers gives the derived class some way of setting sensible values in its state before the base class constructor runs.

btw: IIRC VB.NET puts all derived class construction after the base class construction.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi Jon,

Thanks for your information .. I had a confusion bcoz I am from VC++
background and so was thinking in that terms..

Is it only in the C# or all .Net languages support this functionality.
 
J

Jon Skeet [C# MVP]

Dhananjaya.R said:
Thanks for your information .. I had a confusion bcoz I am from VC++
background and so was thinking in that terms..

Is it only in the C# or all .Net languages support this functionality.

I suspect that the order is language-specific - there's nothing to stop
the compiler from putting the variable initializers after the call to
the base constructor, it's just that C# chooses not to.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top