C# Using singleton instead of a global static instance

Posted by Farstucker on Stack Overflow See other posts from Stack Overflow or by Farstucker
Published on 2010-04-06T22:56:27Z Indexed on 2010/04/06 23:03 UTC
Read the original article Hit count: 261

Filed under:
|
|

I ran into a problem today and a friend recommended I use a global static instance or more elegantly a singleton pattern. I spent a few hours reading about singletons but a few things still escape me.

Background: What Im trying to accomplish is creating an instance of an API and use this one instance in all my classes (as opposed to making a new connection, etc).

There seems to be about 100 ways of creating a singleton but with some help from yoda I found some thread safe examples. ..so given the following code:

public sealed class Singleton
{
     public static Singleton Instance { get; private set; }

     static Singleton() { Instance = new Singleton(); }
}

How/Where would you instantiate the this new class and how should it be called from a separate class?

Thanks for your help.

© Stack Overflow or respective owner

Related posts about c#

Related posts about singleton