In C# 4.0 and later, is it (strictly) defined (in any possible compiler?) when static variables of a class are created, namely their memory allocated? Can I be sure that before entering the constructor of any created instance, a static variable of that class has already been allocated?
Please see the following example:
class X
{
static Vector2 v = new Vector2();
public X()
{
v.Set(1,1); // Can I be sure that memory for v is allocated here ? Always ??
}
}
Is this considered good programming practice?
Yes, you can be sure about that. You can refer to this link. I don’t see the purpose of setting that vector that way, though. Maybe you’d prefer a static constructor, such as: