If a class has a private constructor then it can\'t be instantiated. So, if I don\'t want my class to be instantiated and still use it, then I can make it static.
Wh
Purpose to create the private constructor within a class
To restrict a class being inherited.
Restrict a class being instantiate or creating multiple instance/object.
To achieve the singleton design pattern.
public class TestPrivateConstructor
{
private TestPrivateConstructor()
{ }
public static int sum(int a , int b)
{
return a + b;
}
}
class Program
{
static void Main(string[] args)
{
// calling the private constructor using class name directly
int result = TestPrivateConstructor.sum(10, 15);
// TestPrivateConstructor objClass = new TestPrivateConstructor(); // Will throw the error. We cann't create object of this class
}
}