1
class Test
{
   private static int m = 10;
   private static double n = 20;

   public Test()
   {

   }
}

If a static constructor is used, the static variables are initialized when the first instance of the class is constructed or the first variable is referred. If I don't use a static constructor, when are the static variables initialized and in what order.

SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
Raghav55
  • 3,055
  • 6
  • 28
  • 38

3 Answers3

2

The C# Language Specification, section 10.4.5.1, says:

If a static constructor exists in the class, execution of the static field initializers occurs immediately prior to executing that static constructor. Otherwise, the static field initializers are executed at an implementation-dependent time prior to the first use of a static field of that class.

So, it's implementation-dependent, but all the static fields are guaranteed to be initialized before one of them is used.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • Just look at the generated class with Reflector, you will see the static constructor there. – Aliostad May 10 '11 at 11:28
  • @Aliostad, *some* compilers might do that, but not all, that's why it's implementation-dependent :) – Frédéric Hamidi May 10 '11 at 11:32
  • The static field variable initializers of a class correspond to a sequence of assignments that are executed in the textual order in which they appear in the class declaration. what does the textual order means? – Raghav55 May 10 '11 at 11:32
  • "*some* compilers might do that". Well, I thought we are talking about C# compiler?? :) I said compiler spits out... – Aliostad May 10 '11 at 11:34
  • @Raghav55, it means the order in which the parser encounters them, i.e. the order they're declared in the class. – Frédéric Hamidi May 10 '11 at 11:36
  • @Aliostad, sorry, I should have said *some C# compilers*. – Frédéric Hamidi May 10 '11 at 11:37
  • @Frederic Hamidi: if i have static intitalizers do i need to have static constructor. or in general which is the best way to intitalize the static variables through static intitalizers or through constructors – Raghav55 May 10 '11 at 11:49
  • @Raghav55, it's a matter of personal preference (or company coding rules, which usually take precedence). I personally prefer field initializers, but constructors are fine too :) – Frédéric Hamidi May 10 '11 at 11:54
1

In C#4.0, static fields are initialized as lazy as possible without a static constructor.. While in previous versions, we can't give an exact initialization time. Jon Skeet has a great post about this.

Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
-1

Same place. Compiler spits out the static constructor for you. Order is the one members are defined.


If you look at the class with Reflector, you can see the static constructor:

public class MyStaticClass
{
    public static int MyInt = 10;
}

Becomes:

public class MyStaticClass
{
    // Fields
    public static int MyInt;

    // Methods
    static MyStaticClass();
    public MyStaticClass();
}

With

static MyStaticClass()
{
    MyInt = 10;
}
Aliostad
  • 80,612
  • 21
  • 160
  • 208