473,569 Members | 2,770 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static constructors

Could someone please confirm that static class constructors are only called
at the first
use of a non-static constructor on the class, or am I doing something wrong?
If this is
indeed the case then I would consider this a serious error in the language
implementation,
not to mention a pain in the backside :(

Also, is it to much to ask that the method Type.GetTypeFro mCLSID be
documented
clearly as unimplemented? The help gives the following:

Parameters
clsid
The CLSID of the type to get.
Return Value
System.__ComObj ect regardless of whether the
CLSID is valid.

and then goes on to provide an example of this pointless function in use. I
wouldn't mind
but I have used this function and, of course, it doesn't work.

Andy
Nov 15 '05 #1
9 2422
A J Le Couteur Bisson <no***@nowhere. com> wrote:
Could someone please confirm that static class constructors are only called
at the first use of a non-static constructor on the class, or am I doing
something wrong?
No, they're called either when the first instance is created or when
any static field of the type is referenced.

They can also be called using reflection.
If this is indeed the case then I would consider this a serious error in
the language implementation, not to mention a pain in the backside :(


I think the semantics are fine, and far from a serious error at all.
Are you expecting all the static constructors to be executed when an
assembly loads, in a similar way to some C++ code? If so, then it
sounds like your problem is trying to apply an idiom from one
environment to another.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
100
Hi A J,
Speeking of CLR there is two policies for calling static constructors.
1. By default. The static constructor of a type is called at the first
access to a member of the class(either static or instance).
2. When a type is marked with *beforefieldini t* attribute. CLR is free to
call the type initilizer at the first access to any member of the type or
the type initilizer call can be postponed until the first access to a static
field (not method) of the type. So CLR has more freedom.
Which means that there is no guarantee that when you start creating and
using instances of the class the static constructor is already called. It
might not get called at all.

C# sets *beforefieldini t* attribute for all classes that lack explicit
static constructor declaration and doesn't set it if static constructor is
declared. It does that regardless of wheter static fields have initlializer
expressions on their declarations or not.

The semantics is OK as long as you use type initlizers (static constructors)
to initlize the type. It is not good idea (I believe it is bad design) if
you try to do more work then that in a static constructor.

HTH
B\rgds
100
"A J Le Couteur Bisson" <no***@nowhere. com> wrote in message
news:bn******** **@newsfeed.th. ifl.net...
Could someone please confirm that static class constructors are only called at the first
use of a non-static constructor on the class, or am I doing something wrong? If this is
indeed the case then I would consider this a serious error in the language
implementation,
not to mention a pain in the backside :(

Also, is it to much to ask that the method Type.GetTypeFro mCLSID be
documented
clearly as unimplemented? The help gives the following:

Parameters
clsid
The CLSID of the type to get.
Return Value
System.__ComObj ect regardless of whether the
CLSID is valid.

and then goes on to provide an example of this pointless function in use. I wouldn't mind
but I have used this function and, of course, it doesn't work.

Andy

Nov 15 '05 #3
100 <10*@100.com> wrote:
The semantics is OK as long as you use type initlizers (static constructors)
to initlize the type.


Note that there's a difference between a static constructor and a type
initializer. A static constructor is a C# concept; a type initializer
is a .NET concept.

A class without a static constructor can still have a type initializer
(due to static fields being initialized, etc). I suspect you (100)
understand this, but it's worth keeping the two terms distinct.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
100
Hi Jon,
I think the semantics are fine, and far from a serious error at all.
Are you expecting all the static constructors to be executed when an
assembly loads, in a similar way to some C++ code? If so, then it
sounds like your problem is trying to apply an idiom from one
environment to another.


Just a little correction. C++ has never had static constructors or type
initilizers.

B\rgds
100
Nov 15 '05 #5

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
A J Le Couteur Bisson <no***@nowhere. com> wrote:
Could someone please confirm that static class constructors are only called at the first use of a non-static constructor on the class, or am I doing
something wrong?


No, they're called either when the first instance is created or when
any static field of the type is referenced.

They can also be called using reflection.


This is a helpful observation. I am trying to register business object
types
so that I can reference them by their Guid (In the way that I assume
Type.GetTypeFro mCLSID will eventually work.) I would prefer that
these business object type automatically register by adding themselves
to a Guid->Type hashtable. Scanning the types defined in the assembly
via reflection will do this so all is well

Thanks,
Andy
Nov 15 '05 #6
100 <10*@100.com> wrote:
Hi Jon,
I think the semantics are fine, and far from a serious error at all.
Are you expecting all the static constructors to be executed when an
assembly loads, in a similar way to some C++ code? If so, then it
sounds like your problem is trying to apply an idiom from one
environment to another.


Just a little correction. C++ has never had static constructors or type
initilizers.


Absolutely - but "some C++ code" is executed on startup. I wasn't
meaning to imply that C++ had type initializers or static constructors
like .NET/C# has.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7
100
Thank you Jon,

You are all about details :)

B\rgds
100

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
100 <10*@100.com> wrote:
The semantics is OK as long as you use type initlizers (static constructors) to initlize the type.


Note that there's a difference between a static constructor and a type
initializer. A static constructor is a C# concept; a type initializer
is a .NET concept.

A class without a static constructor can still have a type initializer
(due to static fields being initialized, etc). I suspect you (100)
understand this, but it's worth keeping the two terms distinct.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #8
100
Just a little correction. C++ has never had static constructors or type
initilizers.


Absolutely - but "some C++ code" is executed on startup. I wasn't
meaning to imply that C++ had type initializers or static constructors
like .NET/C# has.

Yes, there is of course startup code (which is big times less than what is
executed when a .NET application starts :-) )
About initializing static fileds - if static fields of class use
initializers that in turn use only constant expressions there won't be any
initlizing code for that particular class.
For example the declaration

class Foo
{
static char* Name;
satitc int IntVal;
static const Const = 10;
};

char* Foo::Name = "Foo class";
int Foo::IntVal = 100 + Const;

won't have any init code behind.

But yes, if you have function calls, non-constant variables or new operator,
c++ compiler will generate initializing code.
B\rgds
100.
Nov 15 '05 #9
In article <eS************ **@tk2msftngp13 .phx.gbl>, 10*@100.com says...
Are you expecting all the static constructors to be executed when an
assembly loads, in a similar way to some C++ code? If so, then it
sounds like your problem is trying to apply an idiom from one
environment to another.


Just a little correction. C++ has never had static constructors or type
initilizers.


So what's the worst problem, then? Applying an idiom from one
environment to another, or making up an environment to accuse someone of
applying to a different environment? ;-)

-- Rick
Nov 15 '05 #10

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

Similar topics

2
3644
by: javac | last post by:
for better or worse, my free time is consumed by two Java series books from Sun: Java Platform Performance, 2000, by Wilson and Kesselman Effective Java, 2001, by Bloch 1.) opinions on these books? good/bad/mediocre/great/etc? are they up to date, or out of date? 2.) please do check out my code at
3
18804
by: Amit | last post by:
is there anything like static constructors or destructors in C++ ? if yes, how to implement it? Thanks, Amit.
7
17186
by: mdc | last post by:
Hi, Is there any way to implement an interface as a static method in C#? If not, is this a bug? Micheal.
6
5887
by: ~~~ .NET Ed ~~~ | last post by:
Yes, I think so at least... In C# you *can* have static properties which are quite useful when used properly. Now imagine the scenario where you need the ability (sp?) to implement a variety of classes that must implement an interface. All these classes must have a particular *static* property, and this in particular is handy not only to be...
17
2337
by: Picho | last post by:
Hi all, I popped up this question a while ago, and I thought it was worth checking again now... (maybe something has changed or something will change). I read this book about component oriented design (owreilly - Juval Lowy), and it was actually very nice. The book goes on about how we should use Interfaces exposure instead of classes...
12
3419
by: Hemanth | last post by:
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...
3
1395
by: Jordan S. | last post by:
Just learning OOP here... Does it make sense to have a constructor in a static class? Say I have a class with a constructor and then mark that class as static; will it (the default constructor) get called when the application starts? or are any/all constructors ignored in static classes? Thanks.
3
4349
by: Adam | last post by:
What happens if one thread is executing a static constructor and another thread starts. Does the second thread block until the first is done in the static constructor? I want to make sure all my globals are initialized and the second thread does not throw an exception. Thanks in advance. Adam Smith
2
1994
by: Tim Van Wassenhove | last post by:
Hello, When i read the CLI spec, 8.10.2 Method inheritance i read the following: "A derived object type inherits all of the instance and virtual methods of its base object type. It does not inherit constructors or static methods...." In the C# spec, 17.2.1 Inheritance i read the following:
0
7694
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...
0
7609
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...
0
7921
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. ...
0
8118
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...
1
7666
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6278
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...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.