Home » Tutorials » C# » Use of Private Constructor in C# with an example

Use of Private Constructor in C# with an example

Difference between Throw and Throw ex in C#
Difference between Abstraction and Encapsulation in C#

Use of Private Constructor in C#

Hey folks, till day you have read Difference between Throw and Throw ex in C#Difference between Var and Dynamic in c#,Difference Between WCF and WebAPIDifference between String and string in C#Difference between Nullable<> and ?,Difference between Hashtable and Dictionary,Insert data into Database table using SQLBulkCopy class in C#Ping in C# , Call method after specific time interval C#Insert Retrieve images from Database in asp.net LINQ TO SQL and Compare two objects in C# . Today we will discuss regarding Use of Private Constructor in C#.

There are basically two types of use of private constructor. Let’s take one by one use with an example and try to understand how can we take advantage of Private constructor in our programming.

Use-1: The class with a Private Constructor can’t be inherited.

As shown in above code snippet we have created “private_constructor_class” class by using Private Constructor and then tried to inherit into public_class. But due protection of class by private constructor we couldn’t inherit that class into any other class and if we do so we could get compile time error as shown below image.





Use-2: Can’t allow to create an object of the class, which have Private Constructor or we must have public constructor with parameters.

As shown in above code snippet we could conclude that we couldn’t create object class in which constructor is declared as Private. But if we have declared overloaded constructor then we could create object of that class by using overloaded method. We could directly access static members/methods of class which has private constructor declared which has been demonstrated in below image.





In a conclusion, try with your own by using given code snippet and try to understand by your own the uses of Private constructor. If you want to understand real implementation of Private Constructor, you will have to study design patterns like factory pattern.



Hello friends, My self Dhruv Sheth, I have more than 6 years of Industrial experience as a Software Developer. In my career I got chance to work with asp.net, c#,Web API, JavaScript, JQuery, WPF, WCF , Windows Application, PostScript, vb.net, GhostScript etc. So I have decided to share my knowledge with everyone and finally I have come up with TechnoThirsty.

Leave a Reply

Your email address will not be published. Required fields are marked *


Time limit is exhausted. Please reload CAPTCHA.

The content is copyrighted to technothirsty.com and may not be reproduced on other websites without permission from the owner.You may contact us using the information below.