Skip to content

Instantly share code, notes, and snippets.

@osya
Last active December 16, 2015 06:52
Show Gist options
  • Save osya/5f5bd696f3d1a7e5b76a to your computer and use it in GitHub Desktop.
Save osya/5f5bd696f3d1a7e5b76a to your computer and use it in GitHub Desktop.
Singleton pattern in C# #CSharp
public class Singleton
{
private static Singleton _singleton;
static Singleton()
{
_singleton = new Singleton();
}
private Singleton()
{
}
public static Singleton Instance
{
get { return _singleton; }
}
public int Count { get; set; }
}
@0xF6
Copy link

0xF6 commented Dec 16, 2015

Compact...

  public class Test
    {
        private Test() { }
        private static Test @this;
        public static Test Instance
        {
            get
            {
                if (@this != null)
                    return @this;
                else
                    return (@this = new Test());
            }
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment