Noffke.com

Singleton

by on Nov.08, 2011, under Software Development

 

Saw a new twist on the Singleton pattern.
Here it is.

 
class ThreadSafeSingleton
{
// typical pattern: private constructor and property to return the instance
private ThreadSafeSingleton() { }

public static ThreadSafeSingleton Instance
{
get { return Nested.instance; }
}

// using this pattern helps make a simple, fast thread-safe singleton that doesn't use a lock
private class Nested
{
static Nested() { }

internal static readonly ThreadSafeSingleton instance = new ThreadSafeSingleton();
}
}
:,

1 Comment for this entry

Share Noffke.com goodness...