Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static field initializers in c#

Tags:

c#

I have following code in c#

class Test
{
  public static int X = Y;    
  public static int Y = 3;    
}

static void Main()
{
Console.WriteLine(Test.X);
Console.WriteLine(Test.Y);
}

In this case I am getting 0 and 3 but in the following case I am getting 3,3

class Test
{
  public static int X = 3;    
  public static int Y = X;    
}

static void Main()
{
Console.WriteLine(Test.X);
Console.WriteLine(Test.Y);
}

why is it so?

like image 301
santosh singh Avatar asked Feb 16 '11 16:02

santosh singh


People also ask

How do you initialize a static field?

The only way to initialize static final variables other than the declaration statement is Static block. A static block is a block of code with a static keyword. In general, these are used to initialize the static members. JVM executes static blocks before the main method at the time of class loading.

What are static initializers in C++?

Static initialization happens first and usually at compile time. If possible, initial values for static variables are evaluated during compilation and burned into the data section of the executable. Zero runtime overhead, early problem diagnosis, and, as we will see later, safe. This is called constant initialization.

What is static variable initialize?

A static variable in a block is initialized only one time, prior to program execution, whereas an auto variable that has an initializer is initialized every time it comes into existence. A static object of class type will use the default constructor if you do not initialize it.

Is a static field initialized in the constructor?

You can define a static field using the static keyword. If you declare a static variable in a class, if you haven't initialized it, just like with instance variables compiler initializes these with default values in the default constructor. Yes, you can also initialize these values using the constructor.


2 Answers

From the C# spec:

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.

Therefore, in your first snippet, since Y isn't initialized yet when X is, X has to default to 0 (because default(int) == 0). On the other hand, in your second snippet, Y can be initialized to the value of X because X has already been given a value of 3 in the immediately-preceding statement.

like image 139
BoltClock Avatar answered Oct 21 '22 11:10

BoltClock


This is per the specification, which states the order for which static fields are initialized. The basic point is that they are initialized in the order that they are declared.

Consequently, in your first snippet, X is initialized first and then Y. As Y has not been initialized yet, it has the default value of 0 and so X gets the value 0.

In your second snippet, X is initialized first but is given the explicit value of 3. Then, Y is initialized and is given the value of X which is 3 since it was initialized first.

From §10.5.5.1 Static field initialization

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. If a static constructor (§10.12) exists in the class, execution of the static field initializers occurs immediately prior to executing that static constructor.

like image 7
jason Avatar answered Oct 21 '22 12:10

jason