473,320 Members | 2,117 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,320 software developers and data experts.

Invoking static constructors in base classes

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
May 30 '06 #1
12 3387
Hemanth wrote:
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.
May 30 '06 #2
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

May 30 '06 #3
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.

"Göran Andersson" <gu***@guffa.com> wrote in message
news:el****************@TK2MSFTNGP04.phx.gbl...
Hemanth wrote:
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.

May 30 '06 #4
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.
--

Stoitcho Goutsev (100)

"Hemanth" <he*****@discussions.microsoft.com> wrote in message
news:CA**********************************@microsof t.com...
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

May 30 '06 #5
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?

Stoitcho Goutsev (100) wrote:
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.

"Göran Andersson" <gu***@guffa.com> wrote in message
news:el****************@TK2MSFTNGP04.phx.gbl...
Hemanth wrote:
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.


May 30 '06 #6
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.
--
HTH
Stoitcho Goutsev (100)

"Göran Andersson" <gu***@guffa.com> wrote in message
news:uM**************@TK2MSFTNGP03.phx.gbl...
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?

Stoitcho Goutsev (100) wrote:
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.

"Göran Andersson" <gu***@guffa.com> wrote in message
news:el****************@TK2MSFTNGP04.phx.gbl...
Hemanth wrote:
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.


May 30 '06 #7
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?

Stoitcho Goutsev (100) wrote:
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.

May 30 '06 #8
Goran,

Responses inline.

"Göran Andersson" wrote:
Hemanth wrote:
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?


http://www.ondotnet.com/pub/a/dotnet...taticxtor.html is one
example. Specifically searching for the string "Base Classes Static
Variables?" refers to what I wrote about.
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.


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
May 31 '06 #9
Hemanth <he*****@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 31 '06 #10
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

"Göran Andersson" wrote:
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?

Stoitcho Goutsev (100) wrote:
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.

May 31 '06 #11
Stoitcho,

Thanks for the response. I shall try this and see if it works.

Hemanth

"Stoitcho Goutsev (100)" wrote:
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.
--

Stoitcho Goutsev (100)

"Hemanth" <he*****@discussions.microsoft.com> wrote in message
news:CA**********************************@microsof t.com...
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


May 31 '06 #12
Hemanth <he*****@discussions.microsoft.com> wrote:
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();
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 31 '06 #13

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

Similar topics

7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
7
by: Beach Potato | last post by:
I guess I've been out of C++ for a while, since right now I don't seem to get a simple solution for overriding inherited constrictors. What worked in Borland C++ & Pascal (and Java, if I remember...
42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
8
by: Ernst Murnleitner | last post by:
Hello Readers, Is there a way that only one class can construct a class A and its inherited classes A2, A3 etc.? I want to construct a class A (and the inherited classes A2, A3 etc.) from a...
13
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. ...
7
by: Sunny | last post by:
Hi all, According C# Language Specification : 10.11 Static constructors: The static constructor for a class executes at most once in a given application domain. The execution of a static...
2
by: Mark | last post by:
I know that you can't have a static virtual property, but is there a way to simulate the same results? I have a base class that I want to extend so that you can change a value and it inherits...
6
by: Tamir Khason | last post by:
I have some classes that basicly do the same things but different way. There are no default constructors in those classes each constructor should recieve same value So Fooclass:MyBasicClass...
6
by: ketan | last post by:
Hi All, I have a situation where in I need to call static methods of the derived class. I checked previous posts, but could not get any satisfactory reply. My situation can be simulated with...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.