When do static variables get initialized in C#?

Oh, that is complex. It depends on whether the beforefieldinit flag is set, which in turn (in C#) depends on whether there is a static constructor. And worse; in .NET 4 I believe the behaviour changed to make it more “lazy” than it used to be.

Frankly, I wouldn’t code to any specific behaviour here; simply: static fields will be initialized before you try to use them, as long as you use regular code to access them.

Jon Skeet has a good write-up on this here

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)