C# ctors 
Author Message
 C# ctors

the sample below, makes me crazy!
why does the static ctor been called on dynamic object and the private ctor
called on static object?
why the order of ctor calls different betwen staticobject and anobject?

public class StaticObject
{
    public static readonly StaticObject Instance = new StaticObject();

    private StaticObject() {Console.WriteLine("private StaticObject ctor");}
    static StaticObject() {Console.WriteLine("static StaticObject ctor");}

    public Method1() {Console.WriteLine("StaticObject.Method1");};

Quote:
}

public class AnObject
{
    public AnObject () {Console.WriteLine("public AnObject ctor");}
    static AnObject () {Console.WriteLine("static AnObject ctor");}

    public Method1 ()
    {
        StaticObject.Instance.Method1();
    }

Quote:
}

void Main ()
{
    AnObject obj = new AnObject();
    obj.Method1();

Quote:
}

result:
static AnObject ctor
public AnObject ctor
private StaticObject ctor
static StaticObject ctor
StaticObject.Method1

--

-----------------------------------------------------------------
Jr?me PIQUOT
WOLFF SA
6, rue de la gare
67120 ERNOLSHEIM SUR BRUCHE
FRANCE

-----------------------------------------------------------------
-----------------------------------------------------------------



Sat, 23 Oct 2004 15:14:49 GMT  
 C# ctors


Wed, 18 Jun 1902 08:00:00 GMT  
 C# ctors
I can't see any problem
Let's go step-step.

static AnObject ctor - called befory instance of AnObject
can be created
public AnObject ctor - AnObject instance created
private StaticObject ct - Instance field causes it by new
operator
static StaticObject cto - called befory instance of
AnObject can be created
StaticObject.Method1  - method call

The only intersting issue is that StaticObject static
constuctor call AFTER instance and not before



Sat, 23 Oct 2004 17:34:01 GMT  
 C# ctors
On Tue, 07 May 2002 04:34:01 -0500, Eliyahu \(Vyacheslav\) Biktagirov

Quote:
> I can't see any problem
> Let's go step-step.

> static AnObject ctor - called befory instance of AnObject can be created
> public AnObject ctor - AnObject instance created private StaticObject ct
> - Instance field causes it by new operator static StaticObject cto -
> called befory instance of AnObject can be created
> StaticObject.Method1  - method call

> The only intersting issue is that StaticObject static constuctor call
> AFTER instance and not before

When I embed the code given previously in a test class, I have:
using System;

class Sample
{

        public class StaticObject
        {
                        public static readonly StaticObject Instance = new StaticObject();

                        private StaticObject() {Console.WriteLine("private StaticObject ctor");}
                        static StaticObject() {Console.WriteLine("static StaticObject ctor");}

                        public void Method1() {Console.WriteLine("StaticObject.Method1");}
        }

        public class AnObject
        {
                        public AnObject () {Console.WriteLine("public AnObject ctor");}
                        static AnObject () {Console.WriteLine("static AnObject ctor");}

                        public void Method1 ()
                        {
                                        StaticObject.Instance.Method1();
                        }
        }

  static        void Main ()
        {
                        AnObject obj = new AnObject();
                        obj.Method1();
        }

Quote:
}

I get the following order:

static AnObject ctor
private StaticObject ctor
static StaticObject ctor
public AnObject ctor
StaticObject.Method1



Sat, 23 Oct 2004 19:38:16 GMT  
 C# ctors
Strange. I just ran this code too and got this output:

static AnObject ctor
public AnObject ctor
private StaticObject ctor
static StaticObject ctor
StaticObject.Method1

-glenn-


Quote:
> On Tue, 07 May 2002 04:34:01 -0500, Eliyahu \(Vyacheslav\) Biktagirov

> > I can't see any problem
> > Let's go step-step.

> > static AnObject ctor - called befory instance of AnObject can be created
> > public AnObject ctor - AnObject instance created private StaticObject ct
> > - Instance field causes it by new operator static StaticObject cto -
> > called befory instance of AnObject can be created
> > StaticObject.Method1  - method call

> > The only intersting issue is that StaticObject static constuctor call
> > AFTER instance and not before

> When I embed the code given previously in a test class, I have:
> using System;

> class Sample
> {

> public class StaticObject
> {
> public static readonly StaticObject Instance = new StaticObject();

> private StaticObject() {Console.WriteLine("private StaticObject ctor");}
> static StaticObject() {Console.WriteLine("static StaticObject ctor");}

> public void Method1() {Console.WriteLine("StaticObject.Method1");}
> }

> public class AnObject
> {
> public AnObject () {Console.WriteLine("public AnObject ctor");}
> static AnObject () {Console.WriteLine("static AnObject ctor");}

> public void Method1 ()
> {
> StaticObject.Instance.Method1();
> }
> }

>   static void Main ()
> {
> AnObject obj = new AnObject();
> obj.Method1();
> }
> }

> I get the following order:

> static AnObject ctor
> private StaticObject ctor
> static StaticObject ctor
> public AnObject ctor
> StaticObject.Method1



Sun, 24 Oct 2004 00:55:12 GMT  
 C# ctors


Wed, 18 Jun 1902 08:00:00 GMT  
 C# ctors

Quote:

> public class StaticObject
> {
>     public static readonly StaticObject Instance = new StaticObject();

>     private StaticObject() {Console.WriteLine("private StaticObject
ctor");}
>     static StaticObject() {Console.WriteLine("static StaticObject ctor");}
<snip>
> result:
> static AnObject ctor
> public AnObject ctor
> private StaticObject ctor
> static StaticObject ctor
> StaticObject.Method1

The problem here is your static instance is actually adding code to your
static constructor.  So, after compling, your code really looks like:

public class StaticObject
{
    public static readonly StaticObject Instance;

    private StaticObject() {Console.WriteLine("private StaticObject ctor");}
    static StaticObject() {
      Instance = new StaticObject();
      Console.WriteLine("static StaticObject ctor");
    }
...

So, you are forcing the call to the instance constructor before the static
constructor can finish.

Ken



Sun, 24 Oct 2004 01:32:32 GMT  
 C# ctors


Wed, 18 Jun 1902 08:00:00 GMT  
 C# ctors

Quote:

> Strange. I just ran this code too and got this output:

> static AnObject ctor
> public AnObject ctor
> private StaticObject ctor
> static StaticObject ctor
> StaticObject.Method1

> -glenn-


>> I get the following order:

>> static AnObject ctor
>> private StaticObject ctor
>> static StaticObject ctor
>> public AnObject ctor
>> StaticObject.Method1

I'm using the Mono C# compiler (http://www.go-mono.org), and you're
probably using a different compiler. Now what I'm not clear about, even
after looking at the C# definition is whether or not the difference in
order violates the standard. I did notice that in some cases the
order is implementation-dependent, but in another case the standard gives
an example and specifically gives a required order of initialization.

I'm going to ask on Mono's mailinglist and see what people say.



Sun, 24 Oct 2004 04:10:35 GMT  
 C# ctors

Quote:
> >> I get the following order:

> >> static AnObject ctor
> >> private StaticObject ctor
> >> static StaticObject ctor
> >> public AnObject ctor
> >> StaticObject.Method1

> I'm using the Mono C# compiler (http://www.go-mono.org), and you're
> probably using a different compiler. Now what I'm not clear about, even
> after looking at the C# definition is whether or not the difference in
> order violates the standard. I did notice that in some cases the
> order is implementation-dependent, but in another case the standard gives
> an example and specifically gives a required order of initialization.

The real requirement for the implicitly ordered constructors is that the
static constructor for a type will be called before any instance
constructors are called (or static fields are accessed).

The exception to the rule is, as I posted elsewhere in this thread, when
your static constructor makes a call to the instance constructor, there is
no way the static constructor can complete before the instance constructor
is called.  Be careful not to access static fields from an instance
constructor that you call in this manner.

Ken



Sun, 24 Oct 2004 04:19:20 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. std::bad_alloc ctor calls malloc????

2. Linker error for .ctor and Finalize in managed classes

3. private copy ctor problem

4. access violation due to bad CString copy ctor when porting from VC6 to VC7 (VC++.NET)

5. User-defined ctor not exported from DLL

6. Throw ignores access level of copy ctor and dtor

7. LINK : error LNK2020: unresolved token (060000EA) CMCNetToken::.ctor

8. C2440 with explicit template ctor

9. Initializing array elements in ctor-initial

10. calling virtual methods from ctor

11. A Jazzy NULL, and ctors for Anonymous Classes

12. CoCreateInstance hangs in CTOR for session class.

 

 
Powered by phpBB® Forum Software