{"id":47488,"date":"2019-02-25T08:25:24","date_gmt":"2019-02-25T07:25:24","guid":{"rendered":"https:\/\/code-maze.com\/?p=47488"},"modified":"2022-01-13T11:07:01","modified_gmt":"2022-01-13T10:07:01","slug":"singleton","status":"publish","type":"post","link":"https:\/\/code-maze.com\/singleton\/","title":{"rendered":"C# Design Patterns &#8211; Singleton"},"content":{"rendered":"<p>The Singleton is a creational design pattern that allows us to create a single instance of an object and to share that instance with all the users that require it. There is a common opinion that the Singleton pattern is not recommended because it presents a code smell, but there are some cases where it fits perfectly.<\/p>\n<p>For example, some components have no reason to be instanced more than once in a project. Take a logger for example. It is quite common to register logger class as a singleton component because all we have to do is to provide a string to be logged and the logger is going to write it to the file. Then multiple classes may require to write in the same file at the same time from different threads, so having one centralized place for that purpose is always a good solution.<\/p>\n<ul id=\"series_parts\" style=\"display: none;\">\n<li><a href=\"https:\/\/code-maze.com\/builder-design-pattern\/\" target=\"_blank\" rel=\"noopener noreferrer\"><span style=\"font-size: 12pt;\">Builder Design Pattern and Fluent Builder<\/span><\/a><\/li>\n<li><a href=\"https:\/\/code-maze.com\/fluent-builder-recursive-generics\/\" target=\"_blank\" rel=\"noopener noreferrer\"><span style=\"font-size: 12pt;\">Fluent Builder Interface With Recursive Generics\u00a0<\/span><\/a><\/li>\n<li><a href=\"https:\/\/code-maze.com\/faceted-builder\/\" target=\"_blank\" rel=\"noopener noreferrer\"><span style=\"font-size: 12pt;\">Faceted Builder\u00a0<\/span><\/a><\/li>\n<li><a href=\"https:\/\/code-maze.com\/factory-method\/\" target=\"_blank\" rel=\"noopener noreferrer\"><span style=\"font-size: 12pt;\">Factory Method\u00a0<\/span><\/a><\/li>\n<li><span style=\"font-size: 12pt;\">Singleton (Current article)<\/span><\/li>\n<li><span style=\"font-size: 12pt;\"><a href=\"https:\/\/code-maze.com\/adapter\/\" target=\"_blank\" rel=\"noopener noreferrer\">Adapter<\/a><\/span><\/li>\n<li><span style=\"font-size: 12pt;\"><a href=\"https:\/\/code-maze.com\/composite\/\" target=\"_blank\" rel=\"noopener noreferrer\">Composite<\/a><\/span><\/li>\n<li><a href=\"https:\/\/code-maze.com\/decorator-design-pattern\/\" target=\"_blank\" rel=\"noopener noreferrer\">Decorator<\/a><\/li>\n<li><a href=\"https:\/\/code-maze.com\/command\/\" target=\"_blank\" rel=\"noopener noreferrer\">Command<\/a><\/li>\n<li><a href=\"https:\/\/code-maze.com\/strategy\/\" target=\"_blank\" rel=\"noopener noreferrer\">Strategy<\/a><\/li>\n<\/ul>\n<p>If you want to see a logger in action in ASP.NET Core Web API, you can read this article: <a href=\"https:\/\/code-maze.com\/net-core-web-development-part3\/\">ASP.NET Core \u2013 Logging with NLog<\/a>.<\/p>\n<p>Or maybe sometimes we have a task to read some data from a file and use them through our project. If we know for sure that file won\u2019t change while we read it, we can create a single instance of the object which will read that file and share it through the project to the consumer classes.<\/p>\n<p>In this article, we are going to show you how to properly implement a Singleton pattern in your project. When we say properly we mean that our singleton class is going to be a <strong>thread-safe which is a crucial requirement when implementing a Singleton pattern.<\/strong><\/p>\n<p>So, let\u2019s begin.<\/p>\n<p>The source code is available at the\u00a0<a href=\"https:\/\/github.com\/CodeMazeBlog\/csharp-design-patterns\/tree\/singleton\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Singleton Design Pattern &#8211; Source Code.<\/a><\/p>\n<p>For the complete list of articles from this series check out <a href=\"https:\/\/code-maze.com\/design-patterns-csharp\/\" target=\"_blank\" rel=\"noopener noreferrer\">C# Design Patterns.<\/a><\/p>\n<h2 id=\"initialproject\">Initial Project<\/h2>\n<p>We are going to start with a simple console application in which we are going to read all the data from a file (which consists of cities with their population) and then use that data. So, to start off, let\u2019s create a single interface:<\/p>\n<pre  class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public interface ISingletonContainer\r\n{\r\n    int GetPopulation(string name);\r\n}\r\n<\/pre>\n<p>After that, we have to create a class to implement the ISingletonContainer interface. We are going to call it SingletonDataContainer:<\/p>\n<pre  class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class SingletonDataContainer: ISingletonContainer\r\n{\r\n    private Dictionary&lt;string, int&gt; _capitals = new Dictionary&lt;string, int&gt;();\r\n\r\n    public SingletonDataContainer()\r\n    {\r\n        Console.WriteLine(\"Initializing singleton object\");\r\n\r\n        var elements = File.ReadAllLines(\"capitals.txt\");\r\n        for (int i = 0; i &lt; elements.Length; i+=2)\r\n        {\r\n            _capitals.Add(elements[i], int.Parse(elements[i + 1]));\r\n        }\r\n    }\r\n\r\n    public int GetPopulation(string name)\r\n    {\r\n        return _capitals[name];\r\n    }\r\n}\r\n<\/pre>\n<p>So, we have a dictionary in which we store the capital names and their population from our file. As we can see, we are reading from a file in our constructor. And that is all good. Now we are ready to use this class in any consumer by simply instantiating it. But is this really what we need to do, to instantiate the class which reads from a file which never changes (in this particular project. Population of the cities is changing daily). Of course not, so obviously using a Singleton pattern would be very useful here.<\/p>\n<p>So, let\u2019s implement it.<\/p>\n<h2 id=\"singletonimplementation\">Singleton Implementation<\/h2>\n<p>To implement the Singleton pattern, let\u2019s change the <code>SingletonDataContainer<\/code> class:<\/p>\n<p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-highlight=\"5,21,23\" data-enlighter-title=\"\">public class SingletonDataContainer: ISingletonContainer\r\n{\r\n    private Dictionary&lt;string, int&gt; _capitals = new Dictionary&lt;string, int&gt;();\r\n\r\n    private SingletonDataContainer()\r\n    {\r\n        Console.WriteLine(&quot;Initializing singleton object&quot;);\r\n\r\n        var elements = File.ReadAllLines(&quot;capitals.txt&quot;);\r\n        for (int i = 0; i &lt; elements.Length; i+=2)\r\n        {\r\n            _capitals.Add(elements[i], int.Parse(elements[i + 1]));\r\n        }\r\n    }\r\n\r\n    public int GetPopulation(string name)\r\n    {\r\n        return _capitals[name];\r\n    }\r\n\r\n    private static SingletonDataContainer instance = new SingletonDataContainer();\r\n\r\n    public static SingletonDataContainer Instance =&gt; instance;\r\n}\r\n<\/pre><\/p>\n<p>So, what we\u2019ve done here is that we hid our constructor from the consumer classes by making it private. Then, we\u2019ve created a single instance of our class and exposed it through the Instance property.<\/p>\n<p>At this point, we can call the Instance property as many times as we want, but our object is going to be instantiated only once and shared for every other call.<\/p>\n<p>Let\u2019s check that theory:<\/p>\n<pre  class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">class Program\r\n{\r\n    static void Main(string[] args)\r\n    {\r\n        var db = SingletonDataContainer.Instance;\r\n        var db2 = SingletonDataContainer.Instance;\r\n        var db3 = SingletonDataContainer.Instance;\r\n        var db4 = SingletonDataContainer.Instance;\r\n    }\r\n}\r\n<\/pre>\n<p>If we start our app, we are going to see this:<\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/02\/01-Singleton-call.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-47491\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/02\/01-Singleton-call.png\" alt=\"Singleton result\" width=\"388\" height=\"151\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/02\/01-Singleton-call.png 388w, https:\/\/code-maze.com\/wp-content\/uploads\/2019\/02\/01-Singleton-call-300x117.png 300w\" sizes=\"auto, (max-width: 388px) 100vw, 388px\" \/><\/a><\/p>\n<p>We can see that we are calling our instance four times but it is initialized only once, which is exactly what we want.<\/p>\n<p>But our implementation is not ideal. Let\u2019s construct our object the lazy way.<\/p>\n<h2 id=\"threadsafesingleton\">Implementing a Thread-Safe Singleton<\/h2>\n<p>Let\u2019s modify our class to implement a thread-safe Singleton by using the <code>Lazy<\/code> type:<\/p>\n<p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-highlight=\"21,23\" data-enlighter-title=\"\">public class SingletonDataContainer : ISingletonContainer\r\n{\r\n    private Dictionary&lt;string, int&gt; _capitals = new Dictionary&lt;string, int&gt;();\r\n\r\n    private SingletonDataContainer()\r\n    {\r\n        Console.WriteLine(&quot;Initializing singleton object&quot;);\r\n\r\n        var elements = File.ReadAllLines(&quot;capitals.txt&quot;);\r\n        for (int i = 0; i &lt; elements.Length; i+=2)\r\n        {\r\n            _capitals.Add(elements[i], int.Parse(elements[i + 1]));\r\n        }\r\n    }\r\n\r\n    public int GetPopulation(string name)\r\n    {\r\n        return _capitals[name];\r\n    }\r\n\r\n    private static Lazy&lt;SingletonDataContainer&gt; instance = new Lazy&lt;SingletonDataContainer&gt;(() =&gt; new SingletonDataContainer());\r\n\r\n    public static SingletonDataContainer Instance =&gt; instance.Value;\r\n}\r\n<\/pre><\/p>\n<p>Right now, our class is completely thread-safe. It is loaded in a lazy way which means that our instance is going to be created only when it is actually needed. We can even check if our object is created with the <code>IsValueCreated<\/code> property if we need to.<\/p>\n<p>Excellent, we have finished our Singleton implementation.<\/p>\n<p>Now we can fully consume it in our consumer class:<\/p>\n<pre  class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">class Program\r\n{\r\n    static void Main(string[] args)\r\n    {\r\n        var db = SingletonDataContainer.Instance;\r\n        Console.WriteLine(db.GetPopulation(\"Washington, D.C.\"));\r\n        var db2 = SingletonDataContainer.Instance;\r\n        Console.WriteLine(db2.GetPopulation(\"London\"));\r\n    }\r\n}\r\n<\/pre>\n<p>Great job.<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>We have seen that even though the Singleton pattern isn\u2019t that much appreciated, it can be helpful in some cases. So, it is always up to us to decide whether we are going to use it or not. Bottom line is that if we need to apply a Singleton pattern in our project, this is a good way to do it.<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Singleton is a creational design pattern that allows us to create a single instance of an object and to share that instance with all the users that require it. There is a common opinion that the Singleton pattern is not recommended because it presents a code smell, but there are some cases where it [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":51911,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[12,504],"tags":[482,493],"class_list":["post-47488","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-csharp","category-design-pattern","tag-design-pattern","tag-singleton","et-has-post-format-content","et_post_format-et-post-format-standard"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C# Design Patterns - Singleton - Code Maze<\/title>\n<meta name=\"description\" content=\"In this article, you are going to learn what is Singleton design pattern, how to implement it into your project and how to create a thread safe singleton.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code-maze.com\/singleton\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C# Design Patterns - Singleton - Code Maze\" \/>\n<meta property=\"og:description\" content=\"In this article, you are going to learn what is Singleton design pattern, how to implement it into your project and how to create a thread safe singleton.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code-maze.com\/singleton\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Maze\" \/>\n<meta property=\"article:published_time\" content=\"2019-02-25T07:25:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-01-13T10:07:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/03\/singleton.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Marinko Spasojevi\u0107\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/CodeMazeBlog\" \/>\n<meta name=\"twitter:site\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Marinko Spasojevi\u0107\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/code-maze.com\/singleton\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/singleton\/\"},\"author\":{\"name\":\"Marinko Spasojevi\u0107\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/d6fa06e66820968d19b39fb63cff2533\"},\"headline\":\"C# Design Patterns &#8211; Singleton\",\"datePublished\":\"2019-02-25T07:25:24+00:00\",\"dateModified\":\"2022-01-13T10:07:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/code-maze.com\/singleton\/\"},\"wordCount\":783,\"commentCount\":7,\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/singleton\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/03\/singleton.png\",\"keywords\":[\"Design Pattern\",\"Singleton\"],\"articleSection\":[\"C#\",\"Design Pattern\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/code-maze.com\/singleton\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/code-maze.com\/singleton\/\",\"url\":\"https:\/\/code-maze.com\/singleton\/\",\"name\":\"C# Design Patterns - Singleton - Code Maze\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/code-maze.com\/singleton\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/singleton\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/03\/singleton.png\",\"datePublished\":\"2019-02-25T07:25:24+00:00\",\"dateModified\":\"2022-01-13T10:07:01+00:00\",\"description\":\"In this article, you are going to learn what is Singleton design pattern, how to implement it into your project and how to create a thread safe singleton.\",\"breadcrumb\":{\"@id\":\"https:\/\/code-maze.com\/singleton\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/code-maze.com\/singleton\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/singleton\/#primaryimage\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/03\/singleton.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/03\/singleton.png\",\"width\":1100,\"height\":620,\"caption\":\"singleton\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/code-maze.com\/singleton\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/code-maze.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C# Design Patterns &#8211; Singleton\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/code-maze.com\/#website\",\"url\":\"https:\/\/code-maze.com\/\",\"name\":\"Code Maze\",\"description\":\"Learn. Code. Succeed.\",\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/code-maze.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/code-maze.com\/#organization\",\"name\":\"Code Maze\",\"url\":\"https:\/\/code-maze.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"width\":3511,\"height\":3510,\"caption\":\"Code Maze\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/CodeMazeBlog\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/d6fa06e66820968d19b39fb63cff2533\",\"name\":\"Marinko Spasojevi\u0107\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/marinko-1x1-3-150x150.jpg\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/marinko-1x1-3-150x150.jpg\",\"caption\":\"Marinko Spasojevi\u0107\"},\"description\":\"Hi, my name is Marinko Spasojevic. Currently, I work as a full-time .NET developer and my passion is web application development. Just getting something to work is not enough for me. To make it just how I like it, it must be readable, reusable, and easy to maintain. Prior to being an author on the CodeMaze blog, I had been working as a professor of Computer Science for several years. So, sharing knowledge while working as a full-time developer comes naturally to me.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/marinko-spasojevic\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog\"],\"url\":\"https:\/\/code-maze.com\/author\/marinko\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C# Design Patterns - Singleton - Code Maze","description":"In this article, you are going to learn what is Singleton design pattern, how to implement it into your project and how to create a thread safe singleton.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/code-maze.com\/singleton\/","og_locale":"en_US","og_type":"article","og_title":"C# Design Patterns - Singleton - Code Maze","og_description":"In this article, you are going to learn what is Singleton design pattern, how to implement it into your project and how to create a thread safe singleton.","og_url":"https:\/\/code-maze.com\/singleton\/","og_site_name":"Code Maze","article_published_time":"2019-02-25T07:25:24+00:00","article_modified_time":"2022-01-13T10:07:01+00:00","og_image":[{"width":1100,"height":620,"url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/03\/singleton.png","type":"image\/png"}],"author":"Marinko Spasojevi\u0107","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/CodeMazeBlog","twitter_site":"@CodeMazeBlog","twitter_misc":{"Written by":"Marinko Spasojevi\u0107","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/code-maze.com\/singleton\/#article","isPartOf":{"@id":"https:\/\/code-maze.com\/singleton\/"},"author":{"name":"Marinko Spasojevi\u0107","@id":"https:\/\/code-maze.com\/#\/schema\/person\/d6fa06e66820968d19b39fb63cff2533"},"headline":"C# Design Patterns &#8211; Singleton","datePublished":"2019-02-25T07:25:24+00:00","dateModified":"2022-01-13T10:07:01+00:00","mainEntityOfPage":{"@id":"https:\/\/code-maze.com\/singleton\/"},"wordCount":783,"commentCount":7,"publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"image":{"@id":"https:\/\/code-maze.com\/singleton\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/03\/singleton.png","keywords":["Design Pattern","Singleton"],"articleSection":["C#","Design Pattern"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code-maze.com\/singleton\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code-maze.com\/singleton\/","url":"https:\/\/code-maze.com\/singleton\/","name":"C# Design Patterns - Singleton - Code Maze","isPartOf":{"@id":"https:\/\/code-maze.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/code-maze.com\/singleton\/#primaryimage"},"image":{"@id":"https:\/\/code-maze.com\/singleton\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/03\/singleton.png","datePublished":"2019-02-25T07:25:24+00:00","dateModified":"2022-01-13T10:07:01+00:00","description":"In this article, you are going to learn what is Singleton design pattern, how to implement it into your project and how to create a thread safe singleton.","breadcrumb":{"@id":"https:\/\/code-maze.com\/singleton\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code-maze.com\/singleton\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/singleton\/#primaryimage","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/03\/singleton.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/03\/singleton.png","width":1100,"height":620,"caption":"singleton"},{"@type":"BreadcrumbList","@id":"https:\/\/code-maze.com\/singleton\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code-maze.com\/"},{"@type":"ListItem","position":2,"name":"C# Design Patterns &#8211; Singleton"}]},{"@type":"WebSite","@id":"https:\/\/code-maze.com\/#website","url":"https:\/\/code-maze.com\/","name":"Code Maze","description":"Learn. Code. Succeed.","publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code-maze.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/code-maze.com\/#organization","name":"Code Maze","url":"https:\/\/code-maze.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","width":3511,"height":3510,"caption":"Code Maze"},"image":{"@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/CodeMazeBlog"]},{"@type":"Person","@id":"https:\/\/code-maze.com\/#\/schema\/person\/d6fa06e66820968d19b39fb63cff2533","name":"Marinko Spasojevi\u0107","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/person\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/marinko-1x1-3-150x150.jpg","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/marinko-1x1-3-150x150.jpg","caption":"Marinko Spasojevi\u0107"},"description":"Hi, my name is Marinko Spasojevic. Currently, I work as a full-time .NET developer and my passion is web application development. Just getting something to work is not enough for me. To make it just how I like it, it must be readable, reusable, and easy to maintain. Prior to being an author on the CodeMaze blog, I had been working as a professor of Computer Science for several years. So, sharing knowledge while working as a full-time developer comes naturally to me.","sameAs":["https:\/\/www.linkedin.com\/in\/marinko-spasojevic\/","https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog"],"url":"https:\/\/code-maze.com\/author\/marinko\/"}]}},"_links":{"self":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/47488","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/comments?post=47488"}],"version-history":[{"count":2,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/47488\/revisions"}],"predecessor-version":[{"id":63376,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/47488\/revisions\/63376"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media\/51911"}],"wp:attachment":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media?parent=47488"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/categories?post=47488"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/tags?post=47488"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}