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. Constructors & Destructors?

Constructors & Destructors?

Scheduled Pinned Locked Moved C#
csharpquestion
5 Posts 3 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.
  • M Offline
    M Offline
    moazzamahmed
    wrote on last edited by
    #1

    hi all, so I know what the constructor does. Is there also a destructor for C# classes? and is it invoked when I do a set myobject = nothing ? also, if Im creating one of those classes that you DONT have to instantiate (what are they called again?), how does the constructor and destructor get called? and is it a good idea to put stuff in the constructor and destructor in those type of classes? thanks Moazzam

    G 1 Reply Last reply
    0
    • M moazzamahmed

      hi all, so I know what the constructor does. Is there also a destructor for C# classes? and is it invoked when I do a set myobject = nothing ? also, if Im creating one of those classes that you DONT have to instantiate (what are they called again?), how does the constructor and destructor get called? and is it a good idea to put stuff in the constructor and destructor in those type of classes? thanks Moazzam

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      moazzamahmed wrote:

      Is there also a destructor for C# classes?

      Yes, it's called Finalize.

      moazzamahmed wrote:

      and is it invoked when I do a set myobject = nothing

      No. It's invoked when the garbage collector is about to remove the object. If you need to do any cleanup in your object, you should inherit the IDisposable interface and implement the Dispose method.

      moazzamahmed wrote:

      also, if Im creating one of those classes that you DONT have to instantiate (what are they called again?), how does the constructor and destructor get called?

      Do you mean a static class? As you don't instantiate it, the constructor or destructor aren't used. Actually, you should declare a private constructor so that noone can create an instance of the class. However, you can also declare a static constructor for the class, that will be called once when the library is loaded. --- b { font-weight: normal; }

      M 1 Reply Last reply
      0
      • G Guffa

        moazzamahmed wrote:

        Is there also a destructor for C# classes?

        Yes, it's called Finalize.

        moazzamahmed wrote:

        and is it invoked when I do a set myobject = nothing

        No. It's invoked when the garbage collector is about to remove the object. If you need to do any cleanup in your object, you should inherit the IDisposable interface and implement the Dispose method.

        moazzamahmed wrote:

        also, if Im creating one of those classes that you DONT have to instantiate (what are they called again?), how does the constructor and destructor get called?

        Do you mean a static class? As you don't instantiate it, the constructor or destructor aren't used. Actually, you should declare a private constructor so that noone can create an instance of the class. However, you can also declare a static constructor for the class, that will be called once when the library is loaded. --- b { font-weight: normal; }

        M Offline
        M Offline
        moazzamahmed
        wrote on last edited by
        #3

        fabulous! thanks Guffa. so whats the use of static classes over non-static? which ones should we use for what TYPE of object? whats better for speed and performance? I was thinking, if a class is basically just utilities, it should be static. But if its something that has properties etc. e.g., a "contact", its should be instantiated. Am I on the right track? thanks again.

        D G 2 Replies Last reply
        0
        • M moazzamahmed

          fabulous! thanks Guffa. so whats the use of static classes over non-static? which ones should we use for what TYPE of object? whats better for speed and performance? I was thinking, if a class is basically just utilities, it should be static. But if its something that has properties etc. e.g., a "contact", its should be instantiated. Am I on the right track? thanks again.

          D Offline
          D Offline
          Dan Neely
          wrote on last edited by
          #4

          moazzamahmed wrote:

          Am I on the right track?

          basically yes. The other thing I'd do statically would be anything that a single copy of is needed app wide. I've got static classes for configuration parameters, and my data wrapper.

          1 Reply Last reply
          0
          • M moazzamahmed

            fabulous! thanks Guffa. so whats the use of static classes over non-static? which ones should we use for what TYPE of object? whats better for speed and performance? I was thinking, if a class is basically just utilities, it should be static. But if its something that has properties etc. e.g., a "contact", its should be instantiated. Am I on the right track? thanks again.

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            moazzamahmed wrote:

            so whats the use of static classes over non-static? which ones should we use for what TYPE of object?

            Use a static class when you don't need to create objects from the class. Generally static classes don't contain any data at all, or if they do, it's data that is common to the entire application.

            moazzamahmed wrote:

            whats better for speed and performance?

            Calling a static method means that a reference to the object isn't included in the call, saving one instruction. This difference is hardly ever anything that you have to be concerned about, though. Concentrate on what gives the best structure to the code, and start thinking about optimizing when you run into performance problems. But even then, there are a lot of other things that are likely to boost performance a lot more than using static methods instead of non-static. --- b { font-weight: normal; }

            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