Possible Duplicate:
What is the need of private constructor in C#?
Hi,
I’ve seen plenty of classes in .NET which have private constructor (Stream is one of them I think). When would I want to model a class like this?
I keep thinking that if my class has no internal state/fields, then I can make it have a private constructor.
Am I on the right track with this idea? I can understand the use of a factory (I’ve run into the tipping point a few times), but not with a private constructor class.
Thanks
Private constructors are normally used for:
System.IO.Streamis not an example of any of these – it has a single, protected constructor (at least as of .NET 4.0) . Since it is an abstract class, it does not make sense for it to have public constructors.In this case, consider creating a static class instead, providing no instance constructors at all (a static class can’t have instance constructors since instances of it cannot be created). Of course, there are cases when this may not be appropriate despite the lack of any state, such as when the class must implement an interface.