C # - Is mixing private constructor and constructor with non-standard parameters allowed in the same class?

Veterans, please forgive me for the stupid question. My understanding is that a class that has a private constructor does not allow instantiation.  

class InterestRate
{
    // static constructor
    static InterestRate()
    {
    }

    //private constructor
    private InterestRate()
    {
    }

    // private overloaded constructor
    private InterestRate(double d1,double d2)
    {
    }

    // overloaded constructor    
    public InterestRate(int a,int b)
    {
        Console.WriteLine("a={0},b={0}",a,b);
    }
}

static void Main()
{
    //As it is private constructor invokation i can not create a instance
    InterestRate firstInstance=new InterestRate();

    // non-private overloaded constructor allow me to craete instance 
    InterestRate r=new InterestRate(10,10);
}

      

My question is that a class with a private constructor prohibits instantiation, why does this class support no private constructor?

+2


source to share


4 answers


Private constructors not only prevent instantiation from the outside world - they often allow building inside a class. Indeed, the only reason a private constructor is often used to prevent instantiation is to avoid the compiler adding a default parameterless public constructor. This is the only way the private constructor gets in the way of other callers.

For example, suppose you have a class that needs to accept a collection in its constructor. A public designer can always act defensively by copying a collection. However, you may have certain cases where the code inside the class wants to create a new instance and knows that it doesn't need to copy the collection, just a reference. This is just one situation in which a private constructor makes a lot of sense.



However, this depends on the specific situation.

+6


source


Adding a private constructor to a class does not mean that you cannot create instances of that class. Instead, it means that you can access the constructor in the context of the class itself (for example, using a static factory method).



+2


source


In the context of this question, constructors act like any other method - the private ones are not available outside the class, but are public. As John pointed out, usually adding a private parameterless constructor simply prevents the compiler from adding a public default parameterless constructor. If another constructor already exists in the class (private parameterless or other), the compiler will not add that default constructor.

So a private constructor doesn't prevent instantiation - it just restricts instantiation to only within the scope of a class method (possibly a public static method). And this only applies to using this private private constructor. Any other constructor allows you to create instances according to its own modifier.

+1


source


Also, if you have a private constructor that takes arguments and a public constructor that takes no arguments, you can still call the private constructor from the public one.

0


source







All Articles