Hi Guys
NP45 public, private & protected
I wish to know only public One() can be called constructor or we can use and call protected One() and private One() as well.
Please anyone knows explain.
Thank you
Hi Guys
NP45 public, private & protected
I wish to know only public One() can be called constructor or we can use and call protected One() and private One() as well.
Please anyone knows explain.
Thank you
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Posted Sep 27, 2007, 1:21 PM
Thank you very much for your explanation, Alan
AlanPosted Sep 27, 2007, 12:42 PM
Hi Maha,
Yes, it's possible to have protected, private and internal instance constructors as well as public ones.
A private constructor would be used where you don't want a class to be externally instantiated and you don't want to inherit from it. However, it could still be called via a static method of the same class which is what generally happens if you're creating a 'singleton' class i.e. a class where only one object will ever be created.
A protected constructor would be used where you don't want a class to be externally instantiated but you do want derived class constructors to be able to access it. It is typically confined to 'abstract' classes.
Finally, an internal constructor would be used where you only want a class to be instantiated within the assembly itself.