C#에서의 Singleton 구현을 위해 Implementing the Singleton Pattern in C#을 읽다가 이상한 것을 발견했다.
public sealed class Singleton
{
static readonly Singleton instance=new Singleton();
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Singleton()
{
}
Singleton()
{
}
public static Singleton Instance
{
get
{
return instance;
}
}
}
Explicit static constructor to tell C# compiler not to mark type as beforefieldinit
이게 무슨 소리?
이게 무슨 소리?
여기에서 인용한 ECMA335 를 보면,
- If marked BeforeFieldInit then the type's initializer method is executed at, or sometime before, first access to any static field defined for that type
- If not marked BeforeFieldInit then that type's initializer method is executed at (i.e., is triggered by):
- first access to any static or instance field of that type, or
- first invocation of any static, instance or virtual method of that type
- first access to any static or instance field of that type, or