Hi
we facing a problem that how we can put a private constructor in a public class if not than give reason.
thanks.....
Can we put a private constructor in my public class if yes then give the example ?
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.


Sam HobbsRank #107 · 20.5K reputationPosted Mar 28, 2012, 6:54 PM EDT
SenthilkumarRank #387 · 6.5K reputationPosted Mar 28, 2012, 7:43 PM EDT
C# allows you to make the constructor as private in the public class.
public class Sample
{
private Sample() //Its valid
{
}
}
If you are trying to create the instance of the class Sample
Sample sample = new Sample(); //It throws error.
But if you have static properties then you are eligible to access those properties. Because it can be accessed without creating the instanced.
For more info,
http://msdn.microsoft.com/en-us/library/kcfb85a6(v=vs.80).aspx