{"id":238,"date":"2013-04-08T09:20:31","date_gmt":"2013-04-08T09:20:31","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=238"},"modified":"2013-04-08T09:42:15","modified_gmt":"2013-04-08T09:42:15","slug":"singleton-in-c","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/singleton-in-c\/","title":{"rendered":"The Singleton Pattern in C#"},"content":{"rendered":"<p>The singleton pattern has fallen out of favour in recent times, the preference being towards using IoC in it&#8217;s place, but it&#8217;s still useful. I&#8217;m not going to go in depth into coding samples for this pattern as there&#8217;s an excellent article <a title=\"Implementing the Singleton Pattern in C#\" href=\"http:\/\/www.yoda.arachsys.com\/csharp\/singleton.html\" target=\"_blank\">&#8220;Implementing the Singleton Pattern in C#<\/a>&#8221; which covers the subject well.<\/p>\n<p>A simple thread safe implementation taken from the aforementioned article is<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic sealed class Singleton\r\n{\r\n   private static readonly Singleton instance = new Singleton();\r\n\r\n   static Singleton()\r\n   {\r\n   }\r\n\r\n   private Singleton()\r\n   {\r\n   }\r\n\r\n   public static Singleton Instance\r\n   {\r\n      get { return instance; }\r\n   }\r\n}\r\n<\/pre>\n<p><em>Note: The private constructor is included as the intention is for the user to use the Instance property to interact with the Singleton instance and not allow creating a new Singleton directly.<\/em><\/p>\n<p><a title=\"System.Lazy\" href=\"http:\/\/msdn.microsoft.com\/en-gb\/library\/dd642331.aspx\" target=\"_blank\">System.Lazy<\/a> has been around since .NET 4.0 and it offers a threadsafe means to implement lazy loading\/initialization. So now we can rewrite the above Singleton class with lazy loading<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic sealed class Singleton\r\n{\r\n   private static readonly Lazy&lt;Singleton&gt; instance = new Lazy&lt;Singleton&gt;(() =&gt; new Singleton());   \r\n\r\n   private Singleton()\r\n   {\r\n   }\r\n\r\n   public static Singleton Instance\r\n   {\r\n      get { return instance.Value; }\r\n   }   \r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The singleton pattern has fallen out of favour in recent times, the preference being towards using IoC in it&#8217;s place, but it&#8217;s still useful. I&#8217;m not going to go in depth into coding samples for this pattern as there&#8217;s an excellent article &#8220;Implementing the Singleton Pattern in C#&#8221; which covers the subject well. A simple [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,2],"tags":[],"class_list":["post-238","post","type-post","status-publish","format-standard","hentry","category-c","category-programming"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=238"}],"version-history":[{"count":8,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/238\/revisions"}],"predecessor-version":[{"id":246,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/238\/revisions\/246"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}