private constructor of a class

Posted by Nishithraj under Regular Expressions on 2/28/2010 | Views : 2795 | Status : [Member] | Replies : 5
I heard like in singleton pattern we should have protected constructor.

Whether we can have private constructor. Whether this is possible for real time??

If it's possible, provide me an example.

Mark this as answer, if it is.....

With regards
Nishithraj Narayanan



Responses

Posted by: Vuyiswamb on: 2/28/2010 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
Good Evening

this will be the best place to start reading


http://msdn.microsoft.com/en-us/magazine/cc188779.aspx

Thank you for posting at Dotnetfunda

Vuyiswa Maseko

Thank you for posting at Dotnetfunda
[Administrator]

Nishithraj, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 2/28/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
Constructors generally cannot be private.

Generally we use constructors to create objects. If you place a constructor as private, you cannot create an object of that particular class. Thus any classes except the nested classes are allowed to create instance of it.

It is really a rare case to use Private Constructors. It is created to classes which have only static members, so that each member can individually be accessed without its object.

See this link :
http://msdn.microsoft.com/en-us/magazine/cc188779.aspx

www.abhisheksur.com

Nishithraj, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nishithraj on: 3/1/2010 [Member] Bronze

Up
0
Down
In that case that class will act like a sealed class. Am I correct??

Mark this as answer, if it is.....

With regards
Nishithraj Narayanan

Nishithraj, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 3/3/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
yes you are ... you cannot inherit that class.

www.abhisheksur.com

Nishithraj, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gttejas on: 3/18/2010 [Member] Starter

Up
0
Down
Hi,

unlike as static class, class with private constructor is not implicitly sealed. you explicitly need to make it sealed otherwise you will get complie time error when try to derive class from that class.






Nishithraj, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response