Empty static constructor is not redundant

Hello,

In R#2.0 build 255, an empty static constructor is marked as redundant,
however the absence of this constructor will cause the class to be
marked with BeforeFieldInit. This might make for subtle different
behavior with respect to the moment of initialisation of the class. Take
a look at http://www.yoda.arachsys.com/csharp/beforefieldinit.html for
more information on the subject.

Kind regards,
Filip.

8 comments
Comment actions Permalink

Thank you for the informantion

--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Filip Duyck" <jetbrains@NOfilip.SPAMduyck.TNXorg> wrote in message
news:ea537a$4v4$1@is.intellij.net...

Hello,

>

In R#2.0 build 255, an empty static constructor is marked as redundant,
however the absence of this constructor will cause the class to be marked
with BeforeFieldInit. This might make for subtle different behavior with
respect to the moment of initialisation of the class. Take a look at
http://www.yoda.arachsys.com/csharp/beforefieldinit.html for more
information on the subject.

>

Kind regards,
Filip.



0
Comment actions Permalink

I believe to have read somewhere that there is a difference between CLR 1.1
and CLR 2.0 and as I recall it having an empty static constructor when you
target CLR 2.0 is redundant as the CLR 2.0 will always create that static
constructor for you and do the initialization of the types within this
static constructor. I tried to find the article but my dear friend Google
didn't give away the answer. I will run some tests this evening to figure
out what it actually was I read....

--
Gabriel Lozano-Morán
The .NET Aficionado
http://www.pointerx.net


"Filip Duyck" <jetbrains@NOfilip.SPAMduyck.TNXorg> wrote in message
news:ea537a$4v4$1@is.intellij.net...

Hello,

>

In R#2.0 build 255, an empty static constructor is marked as redundant,
however the absence of this constructor will cause the class to be marked
with BeforeFieldInit. This might make for subtle different behavior with
respect to the moment of initialisation of the class. Take a look at
http://www.yoda.arachsys.com/csharp/beforefieldinit.html for more
information on the subject.

>

Kind regards,
Filip.



0
Comment actions Permalink

Hello,

Gabriel Lozano-Morán schreef:

I believe to have read somewhere that there is a difference between CLR 1.1
and CLR 2.0 and as I recall it having an empty static constructor when you
target CLR 2.0 is redundant as the CLR 2.0 will always create that static
constructor for you and do the initialization of the types within this
static constructor. I tried to find the article but my dear friend Google
didn't give away the answer. I will run some tests this evening to figure
out what it actually was I read....

Write a small test class with and without the constructor and compare
the MSIL, you'll see the class without static constructor is marked
beforefieldinit. My test setup was a non-static class with a static
constructor and a private instance instructor (singleton pattern stylee).

Filip.

0
Comment actions Permalink

Yes I know that the class will be marked as beforefieldinit but other than
that there is no difference, type initialization is still done in the static
constructor unless Reflector displays it wrongly therefore loosing all the
meaning of the beforefieldinit don't have VS.NET 2003 installed here so I
can not give it a try in 2003

--
Gabriel Lozano-Morán
The .NET Aficionado
http://www.pointerx.net


"Filip Duyck" <jetbrains@NOfilip.SPAMduyck.TNXorg> wrote in message
news:ea5ihe$1al$1@is.intellij.net...

Hello,

>

Gabriel Lozano-Morán schreef:

>> I believe to have read somewhere that there is a difference between CLR
>> 1.1 and CLR 2.0 and as I recall it having an empty static constructor
>> when you target CLR 2.0 is redundant as the CLR 2.0 will always create
>> that static constructor for you and do the initialization of the types
>> within this static constructor. I tried to find the article but my dear
>> friend Google didn't give away the answer. I will run some tests this
>> evening to figure out what it actually was I read....

Write a small test class with and without the constructor and compare the
MSIL, you'll see the class without static constructor is marked
beforefieldinit. My test setup was a non-static class with a static
constructor and a private instance instructor (singleton pattern stylee).

>

Filip.



0
Comment actions Permalink

Gabriel Lozano-Morán schreef:

Yes I know that the class will be marked as beforefieldinit but other than
that there is no difference, type initialization is still done in the static
constructor unless Reflector displays it wrongly therefore loosing all the
meaning of the beforefieldinit don't have VS.NET 2003 installed here so I
can not give it a try in 2003

Yes, but doesn't beforefieldinit decide when the static constructor is
executed (as in "before or on first access" or "always on first access") ?

Filip.

0
Comment actions Permalink

I still believe that this was one of the breaking changes in CLR 2.0 but
can't find the link, I think it saw it in Rotor. Anyway try the code from
Jon Skeet in a ConsoleApp (2.0) and run it multiple times to see the output
and try to do the same in a ConsoleApp targetting CLR 1.1

Could be that I am wrong and if I am, I will buy you a beer after work
tomorrow ;) since we are not sitting that far away from each other...

--
Gabriel Lozano-Morán
The .NET Aficionado
http://www.pointerx.net


"Filip Duyck" <jetbrains@NOfilip.SPAMduyck.TNXorg> wrote in message
news:ea5qhb$s66$1@is.intellij.net...

Gabriel Lozano-Morán schreef:

>> Yes I know that the class will be marked as beforefieldinit but other
>> than that there is no difference, type initialization is still done in
>> the static constructor unless Reflector displays it wrongly therefore
>> loosing all the meaning of the beforefieldinit don't have VS.NET 2003
>> installed here so I can not give it a try in 2003

Yes, but doesn't beforefieldinit decide when the static constructor is
executed (as in "before or on first access" or "always on first access") ?

>

Filip.



0
Comment actions Permalink

After reading the ECMA 335 standard (CLI) I have to admit that I was wrong I
was mixing static constructors (static Foo(){}) with type initializers
(.cctor) which have two totally different semantics and that is what got me
confused. I guess I owe you a bear. I will explain it to you tomorrow what I
mean at work :)

--
Gabriel Lozano-Morán
The .NET Aficionado
http://www.pointerx.net



"Filip Duyck" <jetbrains@NOfilip.SPAMduyck.TNXorg> wrote in message
news:ea5qhb$s66$1@is.intellij.net...

Gabriel Lozano-Morán schreef:

>> Yes I know that the class will be marked as beforefieldinit but other
>> than that there is no difference, type initialization is still done in
>> the static constructor unless Reflector displays it wrongly therefore
>> loosing all the meaning of the beforefieldinit don't have VS.NET 2003
>> installed here so I can not give it a try in 2003

Yes, but doesn't beforefieldinit decide when the static constructor is
executed (as in "before or on first access" or "always on first access") ?

>

Filip.



0
Comment actions Permalink

Hello,

Gabriel Lozano-Morán schreef:

After reading the ECMA 335 standard (CLI) I have to admit that I was wrong I
was mixing static constructors (static Foo(){}) with type initializers
(.cctor) which have two totally different semantics and that is what got me
confused. I guess I owe you a bear. I will explain it to you tomorrow what I
mean at work :)

It's not uncommon to mix them up, last time I checked people were still
discussing on exactly which is which and if there even should be a
difference in terminology.

As for that bear, I think he would be happier if you just let him out in
the woods somewhere :P

See you tomorrow,
Filip.

0

Please sign in to leave a comment.