Constructor order

T

Thomas Scheiderich

I am curious as to 2 things.

One is the order that the constructors are handled and the other is why
the debugger doesn't show the static objects constructor (unless you set
a breakpoint.

For example, here is my code:

**********************************************************************
// DemonstrateDefaultConstructor - demonstrate how default
// constructors work; create a class
// with a constructor and then step
// through a few scenarios
using System;

namespace DemonstrateDefaultConstructor
{
// MyObject - create a class with a noisy constructor
// and an internal object
public class MyObject
{
// this member is a property of the class
static MyOtherObject staticObj = new MyOtherObject();
//int tom = 0;
// this member is a property of the object
MyOtherObject dynamicObj;

public MyObject()
{
Console.WriteLine("MyObject constructor starting");
//
dynamicObj = new MyOtherObject();
Console.WriteLine("MyObject constructor ending");
}
}

// MyOtherObject - this class also has a noisy constructor
// but no internal members
public class MyOtherObject
{
public MyOtherObject()
{
Console.WriteLine("MyOtherObject constructing");
}
}

public class Class1
{
public static void Main(string[] args)
{
Console.WriteLine("Main() starting");
// create an object
MyObject localObject;
Console.WriteLine("Before localObject defined");
localObject = new MyObject();
Console.WriteLine("After localObject new statement");
// wait for user to acknowledge the results
Console.WriteLine("Press Enter to terminate...");
Console.Read();
}
}
}
************************************************************************

The resulting console shows:
***************************************
Main() starting
Before localObject defined
MyOtherObject constructing
MyObject constructor starting
MyObject constructor ending
After localObject new statement
Press Enter to terminate...
**********************************************************************

I am curious as to why the constuctor for MyOtherObject is created
before anything else is done in the MyObject class (including the
MyObject constructor). Here is the order it is handled (according to
the debugger - when you have a breakpoint on Console.Write in the
MyOtherObject constructor.

**************************************************************
localObject
= new MyObject(); // from Main

Console.WriteLine("MyOtherObject constructing"); //MyotherObject
constructer

static MyOtherObject staticObj = new MyOtherObject(); //MyObject
definition

public MyObject() //MyObject Constructor

Console.WriteLine("MyObject constructor starting");//MyObject Constructor
**************************************************************

Also, if you don't set the breakpoint on the MyOtherConstuctor line, it
doesn't even show up when you are tracing through the code (F11).

Thanks,

Tom.
 
J

Jon Skeet [C# MVP]

The resulting console shows:
***************************************
Main() starting
Before localObject defined
MyOtherObject constructing
MyObject constructor starting
MyObject constructor ending
After localObject new statement
Press Enter to terminate...
**********************************************************************

I am curious as to why the constuctor for MyOtherObject is created
before anything else is done in the MyObject class (including the
MyObject constructor).

Because you've got a type initializer (implicitly, due to the
"static MyOtherObject staticObj = new MyOtherObject()" line, which is
being run before anything else in the MyObject class.

You should, however, have *two* lines saying MyOtherObject
constructing, and indeed that's what I get when I run the code.
Also, if you don't set the breakpoint on the MyOtherConstuctor line, it
doesn't even show up when you are tracing through the code (F11).

I don't know about the vagaries of the debugger, I'm afraid. Possibly
it doesn't show type initializers.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi,

Just a little comment.

You would've had 'MyOtherObject constructing' before 'MyObject constructor
starting' even if the first MyOtherObject wasn't static. That is because all
initliazing code is copied at the begining of all instance constructors,
thus all code in the constructor body will follow MyOtherObject creation.
Of course how Jon said there are two 'MyOtherObject constructing' lines.
 
T

Thomas Scheiderich

Jon said:
Because you've got a type initializer (implicitly, due to the
"static MyOtherObject staticObj = new MyOtherObject()" line, which is
being run before anything else in the MyObject class.

You should, however, have *two* lines saying MyOtherObject
constructing, and indeed that's what I get when I run the code.

The reason I don't have 2 "MyOtherObject constructing" messages is
because I had forgot that I had commented out the line:

// dynamicObj = new MyOtherObject();


When I uncomment the line I get the results I think you were talking about:

***************************************************************
Main() starting
Before localObject defined
MyOtherObject constructing
MyObject constructor starting
MyOtherObject constructing
MyObject constructor ending
After localObject new statement
Press Enter to terminate...
***************************************************************

I don't know about the vagaries of the debugger, I'm afraid. Possibly
it doesn't show type initializers.


BTW, how do you tell the MyOtherObject is a class property and not just
another object. It seems to be defined as one? What differentiates it
from a normal Class definition.

Thanks,

Tom.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Thomas,

It is not commented in the code you posted, though.
 
J

Jon Skeet [C# MVP]

Thomas Scheiderich said:
BTW, how do you tell the MyOtherObject is a class property and not just
another object. It seems to be defined as one? What differentiates it
from a normal Class definition.

It's not a property at all - it's a field, and you can tell it's a
static field rather than an instance field because there's the
"static" modifier in front of the field definition.

The class *itself* has nothing special about it at all.
 
T

Thomas Scheiderich

Stoitcho said:
Hi Thomas,

It is not commented in the code you posted, though.


Actually, it is. Maybe you are looking at something different than I am
looking at. Here is section I was looking at (which I copied from my
original post)

*********************************************************
public MyObject()
{
Console.WriteLine("MyObject constructor starting");
// dynamicObj = new MyOtherObject();
Console.WriteLine("MyObject constructor ending");
}

**********************************************************

Were you looking at something else that I missed?

Thanks,

Tom.
 
J

Jon Skeet [C# MVP]

Thomas Scheiderich said:
Actually, it is. Maybe you are looking at something different than I am
looking at. Here is section I was looking at (which I copied from my
original post)

*********************************************************
public MyObject()
{
Console.WriteLine("MyObject constructor starting");
// dynamicObj = new MyOtherObject();
Console.WriteLine("MyObject constructor ending");
}

**********************************************************

Were you looking at something else that I missed?

No, your original post had:

public MyObject()
{
Console.WriteLine("MyObject constructor starting");
//
dynamicObj = new MyOtherObject();
Console.WriteLine("MyObject constructor ending");
}
}

This highlights the need to be careful about formatting code when
posting it.
 
T

Thomas Scheiderich

Jon said:
No, your original post had:

public MyObject()
{
Console.WriteLine("MyObject constructor starting");
//
dynamicObj = new MyOtherObject();
Console.WriteLine("MyObject constructor ending");
}
}

This highlights the need to be careful about formatting code when
posting it.


That's interesting.

Obviously, you copied yours directly from the message as did I. I am
curious as to why yours moved the "dynamicObj" to the next line after
the "//" and mine didn't. I would have expected the same thing with the
"Console.WriteLine" lines, since they started in a later column than the
"dynamicObj" line.

I am using Netscape 6 for my newgroups. Maybe it handles things
differently than yours.

Tom.
 
J

Jon Skeet [C# MVP]

Thomas Scheiderich said:
That's interesting.

Obviously, you copied yours directly from the message as did I. I am
curious as to why yours moved the "dynamicObj" to the next line after
the "//" and mine didn't. I would have expected the same thing with the
"Console.WriteLine" lines, since they started in a later column than the
"dynamicObj" line.

I am using Netscape 6 for my newgroups. Maybe it handles things
differently than yours.

It could be a server on the way. Or Netscape. Or any number of things
:)

Are any of your lines over 80 columns long? If so, that may well be the
problem - try to keep everything to under 80 columns and it should be
fine.
 

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