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.
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.