Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Instanting a class with no constructor

Instanting a class with no constructor

Scheduled Pinned Locked Moved C#
questioncsharpdotnettutorial
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    K Shaffer
    wrote on last edited by
    #1

    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

    P C C 3 Replies Last reply
    0
    • K K Shaffer

      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

      C Offline
      C Offline
      CodingYoshi
      wrote on last edited by
      #2

      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; } }

      K 1 Reply Last reply
      0
      • K K Shaffer

        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

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Shhh... that's private.

        L 1 Reply Last reply
        0
        • C CodingYoshi

          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; } }

          K Offline
          K Offline
          K Shaffer
          wrote on last edited by
          #4

          Ah ha. Private constructors. I knew there had to be a good reason to be able to do that. Thanks

          1 Reply Last reply
          0
          • P PIEBALDconsult

            Shhh... that's private.

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            PIEBALDconsult wrote:

            Shhh... that's private.

            TMI! ;P

            xacc.ide - now with TabsToSpaces support
            IronScheme - 1.0 beta 1 - out now!
            ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

            1 Reply Last reply
            0
            • K K Shaffer

              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

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups