Click here to Skip to main content
12,505,591 members (54,531 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET
PRIVATE Constructor is use in .net or Not?
if YES then in Which Situation we use PRIVATE Constructor?
give me one example?
Posted 25-Jan-13 17:35pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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;
    }
  }
}
  Permalink  
v4
Comments
   
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
Menon Santosh 26-Jan-13 1:28am
   
my +5
Abhinav S 26-Jan-13 3:20am
   
Thank you.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
v2
Comments
Abhinav S 26-Jan-13 0:15am
   
My 5. I've demonstrated a quite example of a private constructor myself.
   
Thank you, Abhinav.
—SA
Menon Santosh 26-Jan-13 1:29am
   
my +5
   
Thank you, Menon.
—SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160927.1 | Last Updated 26 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100