C# Programming->Constructor  
Author Message
DEEPAK GARG





PostPosted: Visual C# Language, C# Programming->Constructor Top

hi

Ques when to use the private constructor. can any one explain with realworld examples



Visual C#7  
 
 
Figo Fei - MSFT





PostPosted: Visual C# Language, C# Programming->Constructor Top

Hi,

A private constructor is a special instance constructor. It is commonly used in classes that contain static members only.

Private constructors are used to prevent the creation of instances of a class when there are no instance fields or methods, such as the Math class, or when a method is called to obtain an instance of a class.

The following is an example of a class using a private constructor.

public class Counter
{
  private Counter() { }
  public static int currentCount;
  public static int IncrementCount()
  {
    return ++currentCount;
  }
}

class TestCounter
{
  static void Main()
  {
    // If you uncomment the following statement, it will generate
    // an error because the constructor is inaccessible:
    // Counter aCounter = new Counter();  // Error

    Counter.currentCount = 100;
    Counter.IncrementCount();
    System.Console.WriteLine("New count: {0}", Counter.currentCount);
  }
}

Reference: http://msdn2.microsoft.com/en-us/library/kcfb85a6(VS.80).aspx

Thank you



 
 
Matthew Watson





PostPosted: Visual C# Language, C# Programming->Constructor Top

The technique of using a private constructor to prevent construction of a an instance of a "static" class has now been superceeded by the "static" class declaration:

public static class MyStaticClass
{
... Static methods and fields only
}

So you should no longer use private constructors for that particular reason.

The most common reason to use a private constructor is when you want calling code to only be able to construct an instance of a class via a so-called "factory method".

A reason for doing this may be because the type of the class constructed may vary depending on the arguments passed in (a way of implementing a so-called "virtual constructor").

To implement a factory method, you often have a public static method in a class that creates and returns an instance of that class. The constructor can then be private.

For example:

public class MyClass
{
public static MyClass Create()
{
MyClass result = new MyClass();
// ... other initialisation
return result;
}

private static MyClass()
{
// .. whatever
}
}

 
 
Rahul Saxena





PostPosted: Visual C# Language, C# Programming->Constructor Top

Realword example cab be, say you want to have the Single connection to the database and want to execute all database operations with that instance only, then you can made you Connection class as Singletion using above mentioned techniques.
 
 
boban.s





PostPosted: Visual C# Language, C# Programming->Constructor Top

As Mattew described, before .NET 2.0 if you had a class all with static methods it was a good practice to put emty private constructor, just to help other developers that use the class to not create instance of it, to be able to use the methods. Why, because if you don't have constructor, builder automatically adds public emty constructor.

More offen you can be in situation where your constructor will be protected internal then private. Sometimes creating new instances can have some logic behind, so direct access to constructor will be hiden or controled by some other class. Only use for private will be when you have other public constructor(s) and you want to hide some specific contrstructor.

 
 
DEEPAK GARG





PostPosted: Visual C# Language, C# Programming->Constructor Top

hi matthew

as in your reply u have written so called " factory method" what is that can u give some real application example where we can use the private constructor.


 
 
DEEPAK GARG





PostPosted: Visual C# Language, C# Programming->Constructor Top

hi matthew

the type of the class constructed may vary depending on the arguments passed in (a way of implementing a so-called "virtual constructor"). so can u also explain me what is "virtual constructor" and if a class taking variable no of arguments then what will happened

with regards

deepak garg


 
 
Matthew Watson





PostPosted: Visual C# Language, C# Programming->Constructor Top

Some info about "Factory Methods":

http://en.wikipedia.org/wiki/Factory_method_pattern

And "Virtual Constructor":

http://en.wikipedia.org/wiki/Virtual_constructor