A private constructor is a special instance constructor. It is generally used in classes that contain static members only. If a class has one or more private constructors and no public constructors, other classes (except nested classes) cannot create instances of this class. class CheckConstructor {// Private Constructor: private CheckConstructor() { }public static int e = 21; }
Private constructor are used in Singleton pattern
Private Constructor will restrict to creat the object of that class. it will leads to protect the data from that class with out accessing from another class.
through private constructor we can achieve the method hiding concept in OOPS
creating object is possible thru public parameterized constructor(if any) of the class having private constructor. However, if the default constructor is private then we can't inherit it.
we cant create object of class
If we are using private constructor in a parent class we cannot create object of parent class and secondly we cannot access the parent class member using child class object
Private Constructor prevent to create an instance of a class. it is mainly use when our class contain only static field or we want to use single ton architecture. class Abs { public static int ab =10; private Abs() { } } class Demo { static void Main(string[] args) { Console.WriteLine(Abs.ab.ToString()); Console.ReadLine(); } }
private constructors are used when there is no need to create the object of the class when we want to create any utility then we can use this private constructor