Instanting a class with no constructor
-
I've come across some classes in the .NET framework that have no constructors defined, but are not static classes. For example, to instantiate an AppDomain class, you call the static function AppDomain.CreateDomain() which returns an instance of the AppDomain class. There are no constructors defined for the class, so you cannot instantiate the class using the new keyword. My question is how does the CreateDomain() function instantiate the class? Thanks in advance, Kevin
-
I've come across some classes in the .NET framework that have no constructors defined, but are not static classes. For example, to instantiate an AppDomain class, you call the static function AppDomain.CreateDomain() which returns an instance of the AppDomain class. There are no constructors defined for the class, so you cannot instantiate the class using the new keyword. My question is how does the CreateDomain() function instantiate the class? Thanks in advance, Kevin
This is sometimes done for Singleton Design Pattern reasons. It is instantiated like so: Public class AppDomain { private AppDomain _instance; // Notice how the constructor is protected (can be private) protected AppDomain() { } // Notice the method is static public static CreateDomain() { // Below it first checks if there is already an instance created and returns it. // Else creates and returns it if (_instance == null) _instance = new AppDomain(); return _instance; } }
-
I've come across some classes in the .NET framework that have no constructors defined, but are not static classes. For example, to instantiate an AppDomain class, you call the static function AppDomain.CreateDomain() which returns an instance of the AppDomain class. There are no constructors defined for the class, so you cannot instantiate the class using the new keyword. My question is how does the CreateDomain() function instantiate the class? Thanks in advance, Kevin
Shhh... that's private.
-
This is sometimes done for Singleton Design Pattern reasons. It is instantiated like so: Public class AppDomain { private AppDomain _instance; // Notice how the constructor is protected (can be private) protected AppDomain() { } // Notice the method is static public static CreateDomain() { // Below it first checks if there is already an instance created and returns it. // Else creates and returns it if (_instance == null) _instance = new AppDomain(); return _instance; } }
-
Shhh... that's private.
-
I've come across some classes in the .NET framework that have no constructors defined, but are not static classes. For example, to instantiate an AppDomain class, you call the static function AppDomain.CreateDomain() which returns an instance of the AppDomain class. There are no constructors defined for the class, so you cannot instantiate the class using the new keyword. My question is how does the CreateDomain() function instantiate the class? Thanks in advance, Kevin
There's no such thing as a class with no constructor, if you don't provide a private one with no arguments, a public one is created. Otherwise, what the other guy said ( factory patterns, etc ). One common use for this would be for a single instance only to exist of a class, so you have a private constructor, in a static class, which has a property that returns the only possible instance of itself. Good for settings, for example.
Christian Graus Driven to the arms of OSX by Vista.