{"id":500,"date":"2014-05-13T21:40:09","date_gmt":"2014-05-13T21:40:09","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=500"},"modified":"2015-08-20T11:32:49","modified_gmt":"2015-08-20T11:32:49","slug":"proper-implementation-of-singleton-in-c","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/windows\/c\/proper-implementation-of-singleton-in-c\/","title":{"rendered":"Proper singleton implementation in C#"},"content":{"rendered":"<p>Taken from: http:\/\/msdn.microsoft.com\/en-us\/library\/ff650316.aspx<\/p>\n<pre class=\"lang:default decode:true\">public class Singleton\r\n{\r\n   private static Singleton instance;\r\n\r\n   private Singleton() {}\r\n\r\n   public static Singleton Instance\r\n   {\r\n      get \r\n      {\r\n         if (instance == null)\r\n         {\r\n            instance = new Singleton();\r\n         }\r\n         return instance;\r\n      }\r\n   }\r\n}<\/pre>\n<p>Static initialization<\/p>\n<pre class=\"lang:default decode:true\">public sealed class Singleton\r\n{\r\n   private static readonly Singleton instance = new Singleton();\r\n   \r\n   private Singleton(){}\r\n\r\n   public static Singleton Instance\r\n   {\r\n      get \r\n      {\r\n         return instance; \r\n      }\r\n   }\r\n}<\/pre>\n<p>Multithreaded singleton<\/p>\n<pre class=\"lang:default decode:true\">public sealed class Singleton\r\n{\r\n   private static volatile Singleton instance;\r\n   private static object syncRoot = new Object();\r\n\r\n   private Singleton() {}\r\n\r\n   public static Singleton Instance\r\n   {\r\n      get \r\n      {\r\n         if (instance == null) \r\n         {\r\n            lock (syncRoot) \r\n            {\r\n               if (instance == null) \r\n                  instance = new Singleton();\r\n            }\r\n         }\r\n\r\n         return instance;\r\n      }\r\n   }\r\n}<\/pre>\n<p>Test program; on first form you have two buttons, one which sets the amount of singleton, and second button which opens up another form which then sets the amount again. Once returned to the first form, the value updates to the one set on second form.<\/p>\n<pre class=\"lang:default decode:true\">\/\/Form1.cs\r\npublic partial class Form1 : Form\r\n{\r\n    myClass mc;\r\n    public Form1()\r\n    {\r\n        InitializeComponent();\r\n        mc = myClass.Instance;\r\n    }\r\n\r\n    private void button1_Click(object sender, EventArgs e)\r\n    {\r\n        mc.iznos = 10;\r\n        updateUI();\r\n    }\r\n\r\n    public void updateUI()\r\n    {\r\n        label1.Text = mc.iznos.ToString();\r\n    }\r\n\r\n    private void button2_Click(object sender, EventArgs e)\r\n    {\r\n        Form2 f2 = new Form2();\r\n        f2.ShowDialog();\r\n    }\r\n\r\n    private void Form1_Activated(object sender, EventArgs e)\r\n    {\r\n        updateUI();\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true\">\/\/Form2.cs\r\npublic partial class Form2 : Form\r\n{\r\n    myClass mc;\r\n    public Form2()\r\n    {\r\n        InitializeComponent();\r\n        \r\n        mc = myClass.Instance;\r\n    }\r\n\r\n    private void Form2_Load(object sender, EventArgs e)\r\n    {\r\n        mc.iznos = 5;\r\n        updateUI();\r\n    }\r\n\r\n    public void updateUI()\r\n    {\r\n        label1.Text = mc.iznos.ToString();\r\n    }\r\n}<\/pre>\n<pre class=\"lang:default decode:true \">public class myClass{\r\n    private static readonly myClass _instance = new myClass();\r\n\r\n    private myClass(){}\r\n    public static myClass Instance\r\n    {\r\n        get \r\n        {\r\n            return _instance; \r\n        }\r\n    }\r\n\r\n    public int iznos { get; set; }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Taken from: http:\/\/msdn.microsoft.com\/en-us\/library\/ff650316.aspx public class Singleton { private static Singleton instance; private Singleton() {} public static Singleton Instance { get { if (instance == null) { instance =&hellip;<\/p>\n","protected":false},"author":1,"featured_media":501,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[],"class_list":["post-500","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/500","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=500"}],"version-history":[{"count":5,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/500\/revisions"}],"predecessor-version":[{"id":2120,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/500\/revisions\/2120"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/501"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=500"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}