I came across a source code with a nested class like this:
public class OuterClass
{
// code of the outer class
protected class NestedClass
{
private string myVar;
private NestedClass() {} // <--- empty private ctor
public NestedClass(string myVar)
{
this.myVar = myVar;
}
}
}
What could be the reason to create this empty private constructor?
I know when implementing a singleton the constructor must be private to prevent other classes to create an instance. However in the case the is another public constructor, so this cannot be the reason.
One reason I thought of would be to not allow the creation of an instance without a value for the variable, that is passed as parameter to the public constructor, but as far as I know this shouldn't be possible when the is simple no other constructor than the public constructor with parameter. Is that correct?
Edit: A answer that was deleted now, mentioned the default constructor. As far as I know a "hidden" default constructor only exists if there is no manually written constructor at all. This is described here:
These constructors are injected into all class declarations that do not introduce other constructors.
Therefore the empty constructor of my nested class, is not a default constructor, but simply a constructor without parameters. I assume that this is the case for top-level classes and nested classes alike.