473,405 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

What will be the constructor flow of control

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( ); }
}

--
Regards
Dhananjaya R
Nov 16 '05 #1
6 1806
Dhananjaya.R <dh**********@NoSpam.com> wrote:
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()

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
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

"Dhananjaya.R" <dh**********@NoSpam.com> wrote in message news:<ef**************@TK2MSFTNGP10.phx.gbl>...
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( ); }
}

Nov 16 '05 #3
Joel Martinez <jd********@ea.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
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.

--
Regards
Dhananjaya R

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Dhananjaya.R <dh**********@NoSpam.com> wrote:
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()

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #5
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.

--
Regards
Dhananjaya R
Nov 16 '05 #6
Dhananjaya.R <dh**********@NoSpam.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Soren Kuula | last post by:
Hi, everyone, I'm beginning work on my Master's thesis in computer science ... I will start out with a pretty advanced, not yet published algorithm for computing quite accurate control flow...
5
by: Franco, Gustavo | last post by:
Hi, I have a question, and please I need a answer. How can I finalize a thread running with Application.Run (I need the message loop!!!) without call Thread.Abort?. I want to call...
9
by: Alvin Bruney [MVP] | last post by:
Exceptions must not be used to control program flow. I intend to show that this statement is flawed. In some instances, exceptions may be used to control program flow in ways that can lead to...
7
by: Dave | last post by:
Hi, I've read a few contradictory statements on how to do flow control between methods. Main problem is that I've seen recommendations (or "standards") to *not* use exceptions for flow control,...
20
by: lars.uffmann | last post by:
I have to work on a rather big project that a bunch of people wrote and that has no useful documentation at all, my C++ has rusted in a bit, now I stumbled over a constructor that looks something...
2
by: brianlum | last post by:
Hi, I have been looking for a good way to convert python code into a control flow graph. I know of Python functions that will convert an expression into an abstract syntax tree (i.e. ast =...
37
by: Erick-> | last post by:
hello!! I was looking at some code in C... and saw this "exotic" operator |=. first time with it, what exactky does??? thanks in advance. Erick->
38
by: Zytan | last post by:
What is the difference between these two lines? Dim args As Object() = New Object() {strText} Dim args As Object() = {strText} args seems usuable from either, say, like so: ...
167
by: darren | last post by:
Hi I have to write a multi-threaded program. I decided to take an OO approach to it. I had the idea to wrap up all of the thread functions in a mix-in class called Threadable. Then when an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.