Information Technology Reference
In-Depth Information
public static
MySingleton
TheOnly
{
get
{
return
theOneAndOnly; }
}
private
MySingleton()
{
}
// remainder elided
}
The singleton pattern can just as easily be written this way, in case you
have more complicated logic to initialize the singleton:
public class
MySingleton2
{
private static readonly
MySingleton2
theOneAndOnly;
static
MySingleton2()
{
theOneAndOnly =
new
MySingleton2
();
}
public static
MySingleton2
TheOnly
{
get
{
return
theOneAndOnly; }
}
private
MySingleton2()
{
}
// remainder elided
}
As with instance initializers, the static initializers are called before any static
constructors are called. And, yes, your static initializers execute before the
base class's static constructor.
The CLR calls your static constructor automatically before your type is
first accessed in an application space (an AppDomain). You can define only