Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Rules of Private Constructor
WhatsApp
Amit Tiwari
11y
16.8
k
0
0
25
Blog
Rules of Private Constructor
Private Constructor is generally used when you don't want to create an object for the class. These are commonly used in classes that contain static members only. If a class has one or more private constructors and no public constructors, then other classes (except nested classes) are not allowed to create instances of this class.
If you do not use an access modifier with the constructor it will still be private by default. The private modifier is usually used explicitly to make it clear that the class cannot be instantiated.
A Private Constructor restricts access to the constructor. It ensures the object can only be created by a member in the type.
We cannot inherit from classes that are having only private constructors. The reason is suppose if you are inheriting from a base class which has only private constructors and if you create an object for the derived class then the base class constructor will be called. Because the base class contains only private constructors and due to ‘private' access modifier it is not accessible from the derived class. So it will give you an error with a syntax as follows ‘Error 1 'ConsoleApplication2.Program.Program()' is inaccessible due to its protection level'
Rules of Private Constructor
Up Next
Private Constructor In C#
Ebook Download
View all
Mastering SOLID Principles in C#
Read by 2.7k people
Download Now!
Learn
View all
Membership not found