Click here to Skip to main content
15,912,837 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have google net very much .. and have some confusion about one thing ....


sealed class- can not inherit these class but create instances

static class- can not create instance and can not inherit them

class with private constructor - can not create instances outside the class . and can not inherit them

abstract class - can not create instance but inherit as a base class .


than whats the use of class with private constructor when same behavior is available in static class . is it only that we create instance of class with private constructor inside the class ..

can any one clear it
Posted

You can't get an instance of a static class while you can get instances of a class having private constructor via static methods.
Private constructor is used, for instance, in the Singleton design pattern (see, for istance "Implementing Singleton in C#"[^]).
 
Share this answer
 
Comments
Gaurav Dixit 3-Dec-14 5:35am    
is there no use of private constructor in traditional asp.net ?
Here is the difference.

Class with private constructor:-

1. You can still have another public constructor to instantiate the class.
2. Private construtor is called every time class has been instantiated.

Static Class:-

1. You cann't instantiate at all.
2. Static constructor is called only once while class is loaded into the memory. or referenced first.
 
Share this answer
 
You can't avail the benefits of all beauty of inheritance but able to achieve the same by using private constructor with static public property (i.e. singleton).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900