Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
PRIVATE Constructor is use in .net or Not?
if YES then in Which Situation we use PRIVATE Constructor?
give me one example?
Posted

A private constructor can occasionally be used in .Net.
An example could be to generate a singleton class - a class which has only instance.

For e.g.

class A
{
  static A o;
  private A()
  {
  }

  public static A getinstance()
  {
    if (o == null) 
    { 
       return o = new A();
    }
    else
    {
       return o;
    }
  }
}
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 26-Jan-13 0:23am    
Aha, a 5, but will you format its indentation to make it readable. Just a note: this is not a perfect singleton, which should not return its instance. For better singleton, please see:

http://csharpindepth.com/Articles/General/Singleton.aspx

But it illustrates the idea well.
—SA
Abhinav S 26-Jan-13 3:27am    
Thank you. Yes I have been through that article before. The Lazy<t> initialization is a new addition - though I'm not familiar with the Lazy keyword.
Menon Santosh 26-Jan-13 1:28am    
my +5
Abhinav S 26-Jan-13 3:20am    
Thank you.
Private constructor is used to disable construction of a class using a constructor. It makes sense for a non-static class, of course. Let's set aside static constructors: they do not allow access modifiers at all, and are always treated as private.

Here is why: if no constructors defined, one default implicitly defined constructor still allows to instantiate this class. Naturally, this constructor is parameterless. So, defining a private parameterless constructor makes it impossible to construct an instance of a class. Other private constructors can be also added.

Now, why? Apparently, if an object cannot be constructed outside of the class, it can be constructed inside. So the purpose of all this activity is actually to create a factory method which returns the instantiated instance of the same class. Apparently, even though all constructors are private, inside class construction can be performed.

Why not a usual way, with non-private constructors? By different reasons an author of code designer may think of, but one of the is essential: because such factory method might return instances of some derived classes. So, compile-time return type is of the same class (which therefore can also be abstract), but run-time types returned are derived (and, of course, never abstract).

For a good example, look at this FCL type: System.Net.WebRequest. It has no constructors at all, but it has a factory method Create. How it works? It does not have public or internal constructor (sorry, not a perfect example: the constructors are not private, but it's still interesting to get an idea). Instead, you use factory methods. It's most interesting to consider the factory method Create(string). It is supposed to be a URI string, and the actual run-time type is determined by the URI scheme (http://en.wikipedia.org/wiki/URI_scheme[^]). Depending of the scheme detected, one of the derived types is constructed and returned: HttpWebRequest, HttpFtpRequest, etc.

Please see:
http://msdn.microsoft.com/en-us/library/system.net.webrequest.aspx[^].

—SA
 
Share this answer
 
v2
Comments
Abhinav S 26-Jan-13 0:15am    
My 5. I've demonstrated a quite example of a private constructor myself.
Sergey Alexandrovich Kryukov 26-Jan-13 0:20am    
Thank you, Abhinav.
—SA
Menon Santosh 26-Jan-13 1:29am    
my +5
Sergey Alexandrovich Kryukov 26-Jan-13 1:39am    
Thank you, Menon.
—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900