tags:

views:

306

answers:

5

I heard that private constructor prevent object creation from outside world.

When i have a code

public class Product
{
   public string Name { get;set;}
   public double Price {get;set;}
   Product()
   {
   }

   public Product(string _name,double _price)
   {
   }
}

here still i can declare public constructor(parameter),won't it spoil the purpose of private constructor? When do we need both private and public constructor(parameter) in code?

I need detailed explanation please.

+5  A: 

The reason you would use the pattern you're describing is when you want to control how the object is instantiated.

In your example, for instance, you're saying the only way to create a Product is by specifying it's name and price. This is with respect to the outside world, of course. You could also do something similar using other access modifiers, and it would have different implications, but it all boils down to controlling how you want the objects instantiated with respect to who will be doing it.

If you wanted to prevent object creation altogether you would have to make all your constructor's private (or protected). That would force the object to be created from within itself (or an inherited class).

Also, as Matti pointed out, when you define a constructor that is parameterized you don't need to specify a private default constructor. At that point it is implied.

Joseph
Defining a constructor with parameters automatically disables the creation of the default parameterless constructor, so in this case separately declaring the parameterless constructor private is not needed.
Matti Virkkunen
A: 

You would use a constructor with parameters when you wanted to force calling code to pass a value to the constructor in order to create an instance of your class. In your example, calling code must use the parameter version of the constructor in order to create a Product.

Thomas
+2  A: 

You need a private constructor when you only want that constructor to be called from within the class itself. In your example you are forcing the calling object to provide 2 parameters when creating the object.

With a private constructor you could do something like:

public static GetInstance ()
{
   return new YourObject();
}

but nothing else except the object could call the parameterless constructor.

It's commonly used to create a singleton pattern:

http://www.dofactory.com/Patterns/PatternSingleton.aspx

Kevin
+2  A: 

Constructors can be chained together, to avoid having to duplicate code, so it's quite common to have private constructors that nobody is supposed to call outside of the class, but that each public constructor then just chains to.

Example:

public class Test
{
    private Test(int? a,string b) { }
    public Test(int a) : this(a, null) { }
    public Test(string b) : this(null, b) { }
}

Here there is two public constructors, one taking a string, and one taking an int, and they both chain to the common private constructor that takes both arguments.

Also, of course, you can constructs new objects from within the same class by using the private constructor, for instance you might want specialized constructors only available through static factory methods:

public static Test Create()
{
    int? a = ReadConfigurationForA();
    string b = ReadConfigurationForB();
    return new Test(a, b);
}

Here it might not be a good idea to expose the private constructor to the outside world, but instead add a static factory method that fetches the correct arguments to pass on the constructor.

Lasse V. Karlsen
A: 

A private constructor is a special instance constructor. It is commonly used in classes that contain static members only. If a class has one or more private constructors and no public constructors, then other classes (except nested classes) are not allowed to create instances of this class.

For more details refer to this: http://msdn.microsoft.com/en-us/library/kcfb85a6(VS.80).aspx

ajdams