Wednesday, 7 August 2013

When are the static constructors executed, before or after static fields?

When are the static constructors executed, before or after static fields?

I wrote
public class ClassA
{
public string PropertyB { get; set; }
public ClassA(string parameterC)
{
this.PropertyB = parameterC;
}
}
public class ClassD
{
static ClassA PropertyE = new ClassA();
static ClassD()
{
this.PropertyE.PropertyB = valueF;
}
}
but it didn't work. Then I rewrote ClassD, and it worked
public class ClassD
{
static ClassA PropertyE = new ClassA { PropertyB = valueF };
}
How are these two codes different?

No comments:

Post a Comment