Invoking static constructors in base classes

G

Guest

Hi,

I have a base class with a static constructor and some abstract methods.
Derived classes implement these methods. From articles on the web, it appears
that there is no guarentee that this static constructor of the base class
would be invoked even if a an object of the derived class is created. Is this
correct ? Is there any way to ensure the base class's static constructor is
invoked before the derived class instance is constructed ?

Thanks
Hemanth
 
G

Guest

Hemanth said:
I have a base class with a static constructor and some abstract methods.
Derived classes implement these methods. From articles on the web,

What articles?
it appears
that there is no guarentee that this static constructor of the base class
would be invoked even if a an object of the derived class is created. Is this
correct ?

Most likely not. The static constructor is invoked when the class is loaded.
 
G

Guest

Read Jon's article about your point
http://www.yoda.arachsys.com/csharp/constructors.html
I have a base class with a static constructor and some abstract methods.
Derived classes implement these methods. From articles on the web, it appears
that there is no guarentee that this static constructor of the base class
would be invoked even if a an object of the derived class is created. Is this
correct ? Is there any way to ensure the base class's static constructor is
invoked before the derived class instance is constructed ?

--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
S

Stoitcho Goutsev \(100\)

Actually there is two types of invoking a type initializer:
1. At some time before, but not later of calling of any method or access any
filed of the type
or
2. At any time before, but not later of accessing any static field of the
type

How the type initializer is called depends on whether the class is marked
with *beforefieldinit* - 1 if not marked and 2 if marked.

I believe if the type is not marked then this will guarantee that the type
initializer will be called before instatiating the type as long as the
instance constructor falls in the category of "calling an instance method".

C# doesn't provide a straight way to specify whether a class needs to be set
as *beforefieldinit* or not, but C# specs state that if a class has a static
construtor it should neve be marked as *beforefieldinit*.

So, if one want to guarantee that the static fields will be initialized
before the class is instantiated, one should declare static constructor even
if the constructor is empty.
 
S

Stoitcho Goutsev \(100\)

Hemanth,

Yes, according to the information you provide there is no guarantee.

If you want to make sure you need to access at least one static field the
base class.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Hm... True. After some further reading I found that:

* The static constructor is called before an instance of the class is
created.

(ECMA 334 section 17.11)

When an instance of the derived class is created, the constructor of the
base class will also be called. Doesn't that guarantee that the static
constructor of the base class has also been called?
 
S

Stoitcho Goutsev \(100\)

This is where the confusion starts.
Static constructor is a language constrcution, .NET types have type
initializers. The difference is that c# class may not have static
constructor and at the same time the type to have type initializer.

Here is an example:

class Foo
{
public static int a = 10;
}

The class doesn't have static constructor, but the type will have type
initializer '.cctor'.

The big difference is that when the class has static constructor it also
will have type initializer, but the spec state that in this case the type
won't be marked as *beforefieldinit* which means the static construcotor
(will be called before instnace is created), however some of the base
classes may have type initializer, but not static constructor. For such base
class I don't think that there is guarantee that the static fields will be
initialized before isntatiating an object.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Yes, I know that, but you are getting away from the original question.

The base class *does* have a static constructor. Hemanth wrote: "I have
a base class with a static constructor".

When the constructor of that base class is called, isn't it certain that
it's static constructor has been executed?
 
G

Guest

Goran,

Responses inline.

Göran Andersson said:
What articles?

http://www.ondotnet.com/pub/a/dotnet/2003/07/07/staticxtor.html is one
example. Specifically searching for the string "Base Classes Static
Variables?" refers to what I wrote about.
Most likely not. The static constructor is invoked when the class is loaded.

My concern was specifically about whether the base class static constructor
is invoked when an object of the derived class is created. I think in other
responses I received, this point is discussed. Let me respond there.

Thanks
Hemanth
 
G

Guest

Goran,

This is exactly the same question that did come to my mind as well. You are
correct - I do have a static constructor in the base class. Both the base and
derived classes have no explicitly defined constructors - so the compiler
would have defined the default constructors for them. So, if the base class
constructor is called when the object is created, I was assuming that the
static constructor would be called as well. That doesn't seem to be happening.

I am not sure what more is required to be done.

Hemanth
 
J

Jon Skeet [C# MVP]

Hemanth said:
I have a base class with a static constructor and some abstract methods.
Derived classes implement these methods. From articles on the web, it appears
that there is no guarentee that this static constructor of the base class
would be invoked even if a an object of the derived class is created. Is this
correct ? Is there any way to ensure the base class's static constructor is
invoked before the derived class instance is constructed ?

An instance of the derived class *is* an instance of the base class, so
I would say that if the static constructor isn't executed, that would
count as a bug in the CLR.
 
J

Jon Skeet [C# MVP]

Hemanth said:
This is exactly the same question that did come to my mind as well. You are
correct - I do have a static constructor in the base class. Both the base and
derived classes have no explicitly defined constructors - so the compiler
would have defined the default constructors for them. So, if the base class
constructor is called when the object is created, I was assuming that the
static constructor would be called as well. That doesn't seem to be happening.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Here's an example which *does* execute both static constructors:

using System;

class Base
{
static Base() { Console.WriteLine ("Base.cctor"); }
}

class Derived : Base
{
static Derived() { Console.WriteLine ("Derived.cctor"); }
}

class Test
{
static void Main()
{
new Derived();
}
}
 

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