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

Leave a Reply

You must be logged in to post a comment.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!