473,734 Members | 2,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3438
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 *beforefieldini t* - 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 *beforefieldini t* or not, but C# specs state that if a class has a static
construtor it should neve be marked as *beforefieldini t*.

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.co m> wrote in message
news:el******** ********@TK2MSF TNGP04.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*****@discus sions.microsoft .com> wrote in message
news:CA******** *************** ***********@mic rosoft.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 *beforefieldini t* - 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 *beforefieldini t* or not, but C# specs state that if a class has a static
construtor it should neve be marked as *beforefieldini t*.

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.co m> wrote in message
news:el******** ********@TK2MSF TNGP04.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 *beforefieldini t* 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.co m> wrote in message
news:uM******** ******@TK2MSFTN GP03.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 *beforefieldini t* - 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 *beforefieldini t* or not, but C# specs state that if a class has a
static construtor it should neve be marked as *beforefieldini t*.

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.co m> wrote in message
news:el******** ********@TK2MSF TNGP04.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 *beforefieldini t* 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*****@discus sions.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.co m>
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

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

Similar topics

7
3664
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. And i have things to offer, and to request. And a lot of ideas, but who needs them.... here's an example (from type_struct.py):
7
13627
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 correctly) as: ------------------------------------------- class Base { public: Base(); }; Base::Base() { printf("Base"); } class Derived : Base { public: Derived(); override; }; Derived::Derived() { /*inherited;*/ printf("Derived"); }
42
5795
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 kind. It sounds simple but evidently .NET has difficulty with this concept for some reason. I do understand that .NET objects are created on the GC heap but that doesn't mean that they couldn't be copied from another object of the same kind when...
8
1898
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 (factory) class Fa. I wanted to make that only F can call new A
13
7732
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. They don't actually protect any encapsulation or anything, and for all the actual protection they offer, they might as well be public. For example: class B { protected:
7
1876
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 constructor is triggered by the first of the following events to occur within an application domain: - An instance of the class is created.
2
3347
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 all of the methods to work with that value. That's easy enough: class BaseC { string val;
6
3286
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 or Fooclass:IBasicClass and I want to be able to call form program something like this:
6
10545
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 following code. // // Does not compile. Not correct C++ syntax - shows what needs to be
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8776
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9182
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.