<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us> said:
Well the class DOES get initialized before calling your static method, if
the class has a static constructor it gets called as well as any static
variable that is declared with an initial value.
Just to be incredibly pedantic, the class *may* not be initialized if
you don't have a static constructor and if the static method doesn't
touch any of the static fields. For instance:
public class Test
{
static string x = Foo("Hello");
public static string Foo (string y)
{
Console.WriteLine (y);
return;
}
}
Calling Test.Foo("Hi")
doesn't *guarantee* that Hello will be printed as well as Hi.
In practice on the .NET CLR it does actually get printed, but the class
is only guaranteed to have its type initializer run at some point
before Test.x is first touched. This is a bit of an odd area...
See
http://www.pobox.com/~skeet/csharp/beforefieldinit.html
for more information. (And yes, it's almost certainly irrelevant in
this case. I just like bringing it up 'cos it's odd
