GurdeepS
GurdeepS

Reputation: 67233

Differences between singleton/static class/instance class with a private ctor?

The below seem to be very similar:

Upvotes: 1

Views: 480

Answers (2)

Oren A
Oren A

Reputation: 5900

Both static constructors (see Fourth version) and private constructors can be used to implement the Singleton design pattern.

Upvotes: 1

Joachim VR
Joachim VR

Reputation: 2340

A static constructor is called the first time the class type is mentioned in any way. Could be used for filling static dictionaries, for example. These constructors can not be called explicitly.

Private constructors on the other hand, can only be called from within the class itself. This can be used to restrict or monitor the creation of new instances of that class, by creating factory methods, for example.

As for the singleton question, Oren A about summed that up.

Upvotes: 0

Related Questions