Types of constructors

S

Sathyaish

What is a private constructor, and why would a class have one? What are
the other kinds of constructors besides:

(1) public constructors; and
(2) parameterized constructors

And I understand that they are not mutually exclusive of one another.
The above classification assimilates my knowledge of having used
constructors in both the above manners.
 
A

Alvin Bruney [MVP - ASP.NET]

private constructors find use in patterns that create only a single instance
of the class - singletons. Have a google for it.

--
Regards,
Alvin Bruney - ASP.NET MVP

[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
Now available @ www.lulu.com/owc, Amazon.com etc
 
F

Francois Bonin [C# MVP]

-You typically define a private constructor when you do not want a class to
be instanciated by the users of your code.
Your class might be a container for constants or a utility class that only
has static methods.

-Public consructors can indeed be parametrized (the visibility of a
constructor has no incidence on whether it should have parameters or no)
-There also are protected constructors (for example the constructor used in
deserializing an object); static constructors which get called before the
very first instance of a class is used.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,084
Latest member
HansGeorgi

Latest Threads

Top