Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static field initialization order (C#) - can someone explain this snippet?

I am a C++ programmer learning C#. I am currently reading C#4.0 in a Nutshell.

I have come accross this statement/snipet on page 74:

Static field initializers run in the order in which the fields are declared. The following example illustrates this: X is initialized to 0 and Y is initialized to 3.

class Foo
{
    public static int X = Y; // 0
    public static int Y = 3; // 3
}

I dont understand how X can be assigned the value in Y, without Y being first declared. Am I missing something here?

As an aside, coming from a C++ background, I tend to use the term ctor for constructor - however, I haven't yet come accross the term in C# - is the term ctor also used in the C# world?

[Edit]

A further example on the same page (in the book mentioned earlier) is this:

class Program
{
    static void Main() { Console.WriteLine (Foo.X); } // 3
}
class Foo
{
    public static Foo Instance = new Foo();
    public static int X = 3;
    Foo() { Console.WriteLine (X); } // 0
}

The book states (for the example above):

The example prints 0 followed by 3 because the field initializer that instantiates a Foo executes before X is initialized to 3:

I have some further questions w.r.t the examples.

  1. Both examples appear under the section entitled Static constructors and field initialization order however, the code examples dont show a static ctor - atleast, not one that I can easily recognise. I was expecting a static ctor to have the same name as the class, be parameterless and be preceeded by the 'static' keyword. So I dont see how the examples relate to the section heading. What am I missing here?

  2. In the second example, the (non static) ctor prints out the value of X - which had explicitly been assigned the value of 3 in the previous line - and yet, the output printed out is 0. Why?!

like image 709
voidstar Avatar asked May 17 '11 06:05

voidstar


People also ask

What is the order of static variable initialization across one program?

Within a single compilation unit, static variables are initialized in the same order as they are defined in the source (this is called Ordered Dynamic Initialization). Across compilation units, however, the order is undefined: you don't know if a static variable defined in a.

Are static variables initialized first?

It is a variable which belongs to the class and not to object(instance ). Static variables are initialized only once, at the start of the execution. These variables will be initialized first, before the initialization of any instance variables.

Which constructor is called first in C# static or default?

A static constructor is called automatically. It initializes the class before the first instance is created or any static members declared in that class (not its base classes) are referenced. A static constructor runs before an instance constructor.

Do static variables get initialized C?

In C, static variables can only be initialized using constant literals.


2 Answers

Order of declaration isn't important in terms of what's available here. It's not like the case where these are local variables, and a variable is only available after it's declared. The same is true for instance variables - and indeed methods, where a method declared earlier in the source can call one declared later in the source, etc.

However, the order of declaration is important in terms of execution of initializers, which is why you're getting that behaviour. Note that if you change these variables to const, both will take the value 3 - the compiler will work out the required order of evaluation, and will detect if there are any cycles (Which would cause an error). The constants would then be evaluated at compile-time, and the values embedded directly in IL.

For more details about what's valid etc, consult the specification.

As for terminology - I don't usually see the need to abbreviate "constructor" to "ctor", although I think it would be generally understood. I might use that abbreviation for a variable name, for example:

var ctor = typeof(...).GetConstructor(...);
like image 197
Jon Skeet Avatar answered Nov 06 '22 11:11

Jon Skeet


In C#, primitive types such as int take on a default value. For int, the default value is 0. This is not like C++ where you have to initialize the value or it will be getting a random dirty value from memory. Since Y is known to be of type int, X can be assigned it's default value of 0.

As far as the keyword ctor, I don't see it used very much as a C# programmer, but I am familiar with the term. I believe it is used in a few places in Visual Studio, for instance in the object browser.

like image 22
mellamokb Avatar answered Nov 06 '22 12:11

mellamokb