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.
What is the use of a private constructor?
Also, it's used in the singleton class, but except for that, is there开发者_StackOverflow any other use?
(Note: The reason I am excluding the singleton case above is that I don't understand why we need a singleton at all when there is a static class available. You may not answer this for my confusion in the question. )
Factory
Private constructors can be useful when using a factory pattern (in other words, a static function that's used to obtain an instance of the class rather than explicit instantiation).
public class MyClass
{
private static Dictionary<object, MyClass> cache =
new Dictionary<object, MyClass>();
private MyClass() { }
public static MyClass GetInstance(object data)
{
MyClass output;
if(!cache.TryGetValue(data, out output))
cache.Add(data, output = new MyClass());
return output;
}
}
Pseudo-Sealed with Nested Children
Any nested classes that inherit from the outer class can access the private constructor.
For instance, you can use this to create an abstract class that you can inherit from, but no one else (an internal
constructor would also work here to restrict inheritance to a single assembly, but the private
constructor forces all implementations to be nested classes.)
public abstract class BaseClass
{
private BaseClass() { }
public class SubClass1 : BaseClass
{
public SubClass1() : base() { }
}
public class SubClass2 : BaseClass
{
public SubClass2() : base() { }
}
}
Base Constructor
They can also be used to create "base" constructors that are called from different, more accessible constructors.
public class MyClass
{
private MyClass(object data1, string data2) { }
public MyClass(object data1) : this(data1, null) { }
public MyClass(string data2) : this(null, data2) { }
public MyClass() : this(null, null) { }
}
As Stefan, Adam and other have pointed out, private constructors are useful in cases where it is undesirable for a class to be created by code outside of the class. Singletons, factories, static method objects are examples of where being able to restrict constructon of a type is useful to enforce a particular pattern.
To respond to the second part of your question about why singletons are needed if static classes exist: singletons and static classes are not equivalent.
For example, a singleton class can implement an interface, a static class cannot. A singleton object may be passed to methods as a parameter - this is not so easy to do with static classes without resorting to wrapper objects or reflection. There are also cases where you may want to create an inheritance hierarchy in which one (or more) of the leaf classes are singleton - this is not possible with static classes either. As another example, you may have several different singletons and you may want to instantiate one of them at runtime based on environmental or configurational parameters - this is also not possible with static classes.
It is important to understand the language features and choose the right one for the job - they're there for a reason.
Sometimes you shouldn't be able to instantiate a class. This makes this explicit and enforces this at the compiler level.
Singletons are just one use case. Constants classes, static methods classes, and other types of patterns dictate that a class should not be instantiable.
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 } }
You can use it to force a singleton instance or create a factory class.
A static method can call the private constructor to create a new instance of that class.
For example a singleton instance:
public class Foo
{
private Foo (){}
private Foo FooInstance {get;set;}
public static Foo GetFooInstance ()
{
if(FooInstance == null){
FooInstance = new Foo();
}
return FooInstance;
}
}
This allows only one instance of the class to be created.
Well if your only objective is that you don't want it to be instantiated, then making it static is sufficient.
If, otoh, you simply don't want it to be instaniated frm outside the class, (maybe you only want users to get one by using a static factory on the class) - then you need a private ctor to allow those publicly accessible static factories to instantiate it.
Historically, remember that making a class static has not always been around... Making the ctor private was a way to make it not-instantiatable (is this a word? ) before the static keyword could be applied to a class...
Regarding singletons - singleton is a design pattern used when the environment and requirements satisfy similar motivations for the pattern's use; static classes are a language feature.
As LBushkin's answer discusses, while some of the goals of using singleton can be met using static classes, a particular implementation of singleton may exceed the feature set of static classes alone.
If the class ONLY has private constructors, it cannot be instantiated from outside.
You can also have private constructors and public constructors with different signatures.
If you want to create a factory for a class, you can use a private constructur, and add some static "factory" methods to the class itself to create the class.
An example for this is the Graphics class, with the From* methods.
Private constructors is a special instance constructor. and are used in some cases where we create a class which only have static members, so creating instance of this class is useless, and that is where private constructor comes into play.
If a class has one or more private constructors and no public constructors, other classes (except nested classes) cannot create instances of this class.
example:
class LogClass {
public static double e = Math.E; //2.71828
// Private Constructor:
private LogClass() {
}
}
The declaration of the empty constructor prevents the automatic generation of a parameter less constructor.
If you do not use an access modifier with the constructor it will still be private by default. Read more: https://qawithexperts.com/tutorial/c-sharp/32/private-constructor-in-c-sharp
精彩评论