When static variable is allocated i.e. when you declare class or at the time of object creation?
-
See this related question: http://stackoverflow.com/questions/218461/difference-initializing-static-variable-inline-or-in-static-constructor-in-c – Dirk Vollmar Dec 21 '09 at 15:31
4 Answers
It is compiled into the static constructor. So the first time anyone creates an object of the class or calls a static method or property on it, the initialization occurs.
Edit: when it is important to you if initialization happens before of after your own static constructor code (and some other edge cases),check the link in the comment by divo.

- 5,045
- 1
- 34
- 44
-
6It's not as simple as that. Jon Skeet has a comprehensive article on that, see http://www.yoda.arachsys.com/csharp/beforefieldinit.html. In classes without static constructors type initializers might be run when the assembly is loaded or when a static method on the type is executed or even only when the first field of the type is accessed. – Dirk Vollmar Dec 21 '09 at 15:29
-
That is a really interesting link, divo. Thanks. I think that in most practical purposes, my answers stands, but there appears to be more to it than I imagined. – Teun D Dec 21 '09 at 15:35
-
Yes, as Jon wrote: "Not many classes actually need the behaviour assumed by many C# programmers - most people need never know the difference, really." But it might be exactly these edge cases that leed to unexpected errors. – Dirk Vollmar Dec 21 '09 at 15:40
-
As other answers have indicated, this will happen in the type (static) constuctor. If your class does not have a type constructor explicitly defined then the compiler will generate one for you. However, determining exactly when this constructor is called is a bit more involved.
If your class does not define an explicit type constructor, e.g.
public class Foo
{
public static int Bar = 1;
}
then the C# compiler will generate a constructor and emit the class definition with the beforefieldinit
flag. This will cause the JIT compiler to guarantee that the type constructor is called sometime before a member of the type is first used but this time is non-deterministic, i.e. it is not possible to know exactly when this will happen, and it could be at a much earlier point than when a type member is first used.
If your class declares an explicit type constructor, e.g.
public class Foo
{
public static int Bar;
static Foo()
{
Bar = 1;
}
}
then the compiler will emit IL for the class without the beforefieldinit
flag. In this case the JIT compiler will call the type constructor at a deterministic time, i.e. immediately before the first type member access.
The former JIT behaviour is known as before-field-init semantics and the latter as precise sematntics. It is important to know the difference between the two since, in some scenarios, they may have a significant performance implication.

- 29,453
- 4
- 60
- 67
-
A nitpick: What matters for `beforefieldinit` is whether or not a static constructor exists at all, *not* whether your fields are initialised inside the constructor. If the class has no static constructor then it will be marked as `beforefieldinit` by the compiler; if the class has a static constructor then it won't be marked as `beforefieldinit`, regardless of where its fields are initialised. – LukeH Dec 21 '09 at 15:51
-
@Luke - thanks for pointing that out. Nitpicking is good ;-) have edited – Adam Ralph Dec 22 '09 at 07:17
Static variables are allocated as soon as static (type) constructor is called. This happens when you call any method that reference the type for the first time, before method execution.

- 8,368
- 7
- 35
- 44
-
-
Any explicit fields initialization is compiled-in to the constructor before the constructor body. If there is no declared static constructor compiler will generate one. – ironic Dec 21 '09 at 15:52
-
@ironic: No, this is not the case. If there is no static constructor the class will be marked with the `beforefieldinit` flag meaning the time when the type initializer is executed is not deterministic. – Dirk Vollmar Dec 21 '09 at 16:03