Skip to content

Instantly share code, notes, and snippets.

@MechMK1
Created February 17, 2015 15:41
Show Gist options
  • Select an option

  • Save MechMK1/ca08d51fc936988b7b8a to your computer and use it in GitHub Desktop.

Select an option

Save MechMK1/ca08d51fc936988b7b8a to your computer and use it in GitHub Desktop.
C# Singletons
class Singleton
{
//Private static field to hold the instance
private static Singleton _singleton = null;
// Getter for the instance
public static Singleton Instance
{
get
{
//Return the instance if not null (see ??-operator)
return _singleton ?? (
_singleton = new Singleton() //Create instance, assign and return it
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment