thecodewerks

thecodewerks
- Name: [not set]
- Favorite Languages: [not set]
- Website: [not set]
- Location: [not set]
- About Me: [not set]
-
C# Tutorial - Singleton Pattern
09/15/2010 - 13:24
Here is my spin on it as an inheritable generic singleton class
public class Singleton<T> where T : class, new()
{
static readonly T instance = new T();
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Singleton() { }
protected Singleton() { }
public static T Instance { get { return instance; } }
}
public class MySingleton : Singleton<MySingleton>
{
// seeded so new intances will always produce the same first number
private Random random = new Random(10);
public double Number { get { return random.NextDouble(); } }
}
Recent Comments