
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to implement a Singleton design pattern in C#?
Singleton Pattern belongs to Creational type pattern
Singleton design pattern is used when we need to ensure that only one object of a particular class is Instantiated. That single instance created is responsible to coordinate actions across the application.
As part of the Implementation guidelines we need to ensure that only one instance of the class exists by declaring all constructors of the class to be private. Also, to control the singleton access we need to provide a static property that returns a single instance of the object.
Example
Sealed ensures the class being inherited and object instantiation is restricted in the derived class
Private property initialized with null
ensures that only one instance of the object is created
based on the null condition
Private constructor ensures that object is not instantiated other than with in the class itself
Public method which can be invoked through the singleton instance
public sealed class Singleton { private static int counter = 0; private static Singleton instance = null; public static Singleton GetInstance { get { if (instance == null) instance = new Singleton(); return instance; } } private Singleton() { counter++; Console.WriteLine("Counter Value " + counter.ToString()); } public void PrintDetails(string message) { Console.WriteLine(message); } } class Program { static void Main() { Singleton fromFacebook = Singleton.GetInstance; fromFacebook.PrintDetails("From Facebook"); Singleton fromTwitter = Singleton.GetInstance; fromTwitter.PrintDetails("From Twitter"); Console.ReadLine(); } }
Output
Counter Value 1 From Facebook From Twitter
- Related Articles
- Explain C++ Singleton design pattern.
- How to implement IDisposable Design Pattern in C#?
- What is proxy design pattern and how to implement it in C#?
- How to prevent Cloning to break a Singleton Class Pattern?
- How to prevent Reflection to break a Singleton Class Pattern?
- How to prevent Serialization to break a Singleton Class Pattern?
- How to implement Null object Pattern in C#?
- How to prevent Cloning to break a Singleton Class Pattern in Java?
- Composite Design Pattern in C++
- What is singleton design concept in PHP?
- How to implement Builder pattern in Kotlin?
- How to write a singleton class in C++?
- Design Pattern in Dart
- Types of Pattern Design
- Singleton Class in C#
