ππ¦π©π₯ππ¦ππ§ππππ’π¨π§ π¨π ππ’π§π π₯πππ¨π§ ππ₯ππ¬π¬ π’π§ π# (ππ‘π«πππ ππππ)
πΎππ πΊπππππ ? We want only one Instance of Singleton class , when a class inherits a call to parent constructor comes which isβ¦
In this topic we would learn how to implement Singleton Design Pattern using C#Β .
Look at the code this is the complete code for a Singleton Class thread safe.
πΎππ πΊπππππ
Β ?
We want only one Instance of Singleton classΒ , when a class inherits a call to parent constructor comes which is eventually causing a object creation (Kind of objection creation not exactly). So to avoid it we have made Singleton class sealed. Now no other class can inherit from it.
πΎππ πͺππππππππππ ππ π·ππππππΒ ?
To avoid multiple instance creation of Singleton class we have made constructor private. When constructor is private no one can create object of that class using new Singleton() out of that class.
π°π πͺππππππππππ ππ π·ππππππ π―ππ π°πππππππ ππ πππππ πͺππππππ
Β ?
We have created a getter method which is responsible for instance creation. Because constructor is private and we canβt call this method through object of that classΒ , thatβs why we are obliged to make this method static ( We can call static method directly with Class name without object creation)
πΎππ ππππΒ ?
Lock ensures ensures that one thread is executing a piece of code at one time.
If you like my work you can help me in growing through following ways