Click here to Skip to main content
Rate this: bad
good
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 3

  Permalink  
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
Sergey Alexandrovich Kryukov at 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 at 26-Jan-13 3:27am
   
Thank you. Yes I have been through that article before. The Lazy initialization is a new addition - though I'm not familiar with the Lazy keyword.
Menon Santosh at 26-Jan-13 1:28am
   
my +5
Abhinav S at 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 at 26-Jan-13 0:15am
   
My 5. I've demonstrated a quite example of a private constructor myself.
Sergey Alexandrovich Kryukov at 26-Jan-13 0:20am
   
Thank you, Abhinav. —SA
Menon Santosh at 26-Jan-13 1:29am
   
my +5
Sergey Alexandrovich Kryukov at 26-Jan-13 1:39am
   
Thank you, Menon. —SA

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 940
1 OriginalGriff 543
2 Abhinav S 253
3 thatraja 245
4 Emre Ataseven 200
0 Sergey Alexandrovich Kryukov 8,282
1 OriginalGriff 4,750
2 Peter Leow 3,784
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 26 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid