Cyberiafreak

"Fortunate are those who take the first steps.” ― Paulo Coelho

Implementing a Singleton Class The Right way

Cleaner option:

public class MySingleton()
{
private MySingleton() {} //Private cstr
public static readonly MySingleton Instance = new MySingleton();
}

The class will be constructed the first time MySingleton.Instance is called and will be get-only because of readonly.  This is also thread-safe without any locks (handled  by the CLR).  This is a Poor Man’s singleton, because serious applications will have an IoC container to provide singletons.

Copied from here

Advertisements

May 2, 2013 - Posted by | .NET General

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: