
- 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
Why singleton class is always sealed in C#?
The sealed keyword means that the class cannot be inherited from. Declaring constructors private means that instances of the class cannot be created.
You can have a base class with a private constructor, but still inherit from that base class, define some public constructors, and effectively instantiate that base class.
Constructors are not inherited (so the derived class won't have all private constructors just because the base class does), and that derived classes always call the base class constructors first.
Marking the class sealed prevents someone from trivially working around your carefully-constructed singleton class because it keeps someone from inheriting from the class.
Example
static class Program { static void Main(string[] args){ Singleton fromStudent = Singleton.GetInstance; fromStudent.PrintDetails("From Student"); Singleton fromEmployee = Singleton.GetInstance; fromEmployee.PrintDetails("From Employee"); Console.WriteLine("-------------------------------------"); Singleton.DerivedSingleton derivedObj = new Singleton.DerivedSingleton(); derivedObj.PrintDetails("From Derived"); Console.ReadLine(); } } public class Singleton { private static int counter = 0; private static object obj = new object(); private Singleton() { counter++; Console.WriteLine("Counter Value " + counter.ToString()); } private static Singleton instance = null; public static Singleton GetInstance{ get { if (instance == null) instance = new Singleton(); return instance; } } public void PrintDetails(string message){ Console.WriteLine(message); } public class DerivedSingleton : Singleton { } }
- Related Articles
- What is a sealed class in C#?
- Singleton Class in C#
- Abstract vs Sealed Classes vs Class Members in C#
- What is a singleton class in Java?
- How to write a singleton class in C++?
- What is the difference between Static class and Singleton instance in C#?
- How to use singleton class in android?
- How we can create singleton class in Python?
- What are sealed modifiers in C#?
- How to make a class singleton in Java?\n
- How to refresh Singleton class every one hour in android?
- Why is __init__() always called after __new__() in python?
- Why the main () method in Java is always static?
- How to prevent Cloning to break a Singleton Class Pattern?
- How to prevent Reflection to break a Singleton Class Pattern?

Advertisements