Javatpoint Logo
Javatpoint Logo

Private constructor in C#

In C#, a Private Constructor is generated by utilizing the Private Access Specifier. When a class has a private constructor and no additional Public Constructors, we cannot construct an object for that class outside of the class. However, we can create objects for the classes within the same class.

So, the most important thing to remember is that constraints such as those for generating objects, accessing members, and so on can come into play when we try to create objects and get to members from outside of the class.

Creating an Object in C# Using a Private Constructor Inside the Same Class:

An instance cannot be created from outside the class. However, it can be created within the class. Take a look at the example below for more information. We defined the private constructor within the Program class, but the Main method is also defined in an identical Program class. As we can see, we are generating an instance of the Program class and running Method1 inside the Main method.

Filename: Constructor.c


This shows the Private Constructor
The Method1 is get Called

Creating an Instance in C# from Outside the Class

The most significant thing to remember is that the class should have a public constructor so that we can create an instance independently of the class. It makes no difference whether a class has a private constructor or not. Still, if a class has a public constructor, we can generate the class instance and execute the public non-static functions using that public constructor.

Take a look at the example below for a better understanding. The Test class has both Private and Public constructors and a public function. We are now creating an instance of the Test class and running the Method1 method using the Program class Main function (independent of the Test class).



Public constructor
The Method1 is get Called

What happens if the class skips a public constructor and has a private constructor? Then, we won't be able to create an instance outside the class. Refer to the example listed below for a better understanding. The Test class has just a private constructor, and when we create an instance for the Test class using the Private constructor within the Main method, we encounter a compile-time error.



prog.cs(9,25): error CS0122: `PrivateConstructorDemo.Tests.Tests()' is inaccessible due to its protection level
prog.cs(17,17): (Location of the symbol related to previous error)
Compilation failed: 1 error(s), 0 warnings


As you can see, we are getting the message that 'Test.Tests()' is inaccessible because of its protective level, which makes sense considering the Tests class Tests() constructor in the Program class is available due to its security level, which is private.

The first thing to remember is that a private constructor limits the class from being instantiated by anyone other than the class if it lacks a public constructor. We can create a new instance outside the class if it contains a public constructor. There are no limitations on generating the instance within the same class.

Use Case: If we don't want the class to be able to be instantiated from outside the class, add a private constructor in place of any public constructors in the class.

Private Constructor Restricting Inheritance in C#:

It indicates that a class cannot be inherited without a private constructor. It is partially correct as well. Let us demonstrate this with a few examples.

It is feasible to inherit the class if it has a private constructor and any other public constructor. The child class requires a publicly available constructor to form a parent-child relationship. That is, irrespective of whether the class has a private constructor, we can inherit it if it has a public constructor.

Take a look at the sample below for a better understanding. Within the Parent class, we have two constructors for objects, one private and one public.



The Public constructor of the parent class
The Public constructor of the child class

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA