What is a private constructor? where will you use it?
Answers were Sorted based on User's Feedback
Answer / ramkishore mateti
When you declare a Constructor with Private access modifier
then it is called Private Constructor.
If you declare a Constructor as private then it don't allow
to create object for its derived class,i.e you loose
inhirect facility for that class.
Ex:
Class A
{
// some code
Private Void A()
{
//Private Constructor
}
}
Class B:A
{
//code
}
B obj = new B();// will give Compilation Error
Because Class A construcor declared as private hence its
accessbility limit is to that class only ,Class B can't
access. As i explained the heirarchy of Constructors in the
previous qn, when we create a object for Class B that
constructor will call constructor A but class B have no
rights to access the Class A constructor hence we will get
compilation error.
| Is This Answer Correct ? | 29 Yes | 1 No |
Answer / nitesh
When we want to control the object instantiation
or trying to implement singleton pattern
| Is This Answer Correct ? | 10 Yes | 2 No |
Answer / dsr
private constructor access only that class. we can't access
in other class.we can use the private constructor in
singleton patteren.
| Is This Answer Correct ? | 5 Yes | 0 No |
Answer / nevin jain
In singleton pattern we make the class sealed n keep
contractor as protected
......which is same as keeping class with any access
modifier with private constructor..
both pattern is the example of singleton pattern..
basically this pattern say that only single instance can be
created of the class which can be achieved either by private
constructor or sealed class with protected constructor..
| Is This Answer Correct ? | 3 Yes | 0 No |
Answer / saurabh
In singleton pattern the constructor is not private but
protected.
Go for private constructor when your class has methods which
do not require object creation. ex. (Math class in c#).
Like If your class has all static method, you need not to
create an object in that case go for private constructor.
| Is This Answer Correct ? | 2 Yes | 1 No |
Answer / sri
we can declare the constructor private ,it is called
private constructor.
| Is This Answer Correct ? | 3 Yes | 15 No |
How can we insert 100 records @ a time without using for loop into the databse
Can we call server-side code (c# or vb.net code) from javascript?
Are tuples immutable c#?
What?s the implicit name of the parameter that gets passed into the class? set method?
Is string primitive?
Is string a class in c#?
Write one code example for compile time binding and one for run time binding? What is early/late binding?
Can int be null in c#?
What is deadly diamond problem?
What is continue in c#?
What does the keyword virtual mean in the method definition?
Can you change the value of a variable while debugging a C# application?
Visual Basic (800)
C Sharp (3816)
ASP.NET (3180)
VB.NET (461)
COM+ (79)
ADO.NET (717)
IIS (369)
MTS (11)
Crystal Reports (81)
BizTalk (89)
Dot Net (2435)
Exchange Server (362)
SharePoint (720)
WCF (340)
MS Office Microsoft (6963)
LINQ Language-Integrated Query (317)
WPF (371)
TypeScript (144)
Microsoft Related AllOther (311)