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. deleting class objects

deleting class objects

Scheduled Pinned Locked Moved C#
csharpquestion
3 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
    Manu_81
    wrote on last edited by
    #1

    Hi, I want to delete a C# class object and all the resources used by the class before the garbage collector does it. How do I do it in C#. Thanks.

    T G 2 Replies Last reply
    0
    • M Manu_81

      Hi, I want to delete a C# class object and all the resources used by the class before the garbage collector does it. How do I do it in C#. Thanks.

      T Offline
      T Offline
      Tom Larsen
      wrote on last edited by
      #2

      If the object represents a truly limited resource, it should implement IDisposable and then one would call `object.Dispose()` when they are done with it. If it isn't a limited resource that needs active maintaince then let the system handle it by normal garbage collection.

      1 Reply Last reply
      0
      • M Manu_81

        Hi, I want to delete a C# class object and all the resources used by the class before the garbage collector does it. How do I do it in C#. Thanks.

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

        You don't. If the class inherits IDisposable, e.g. has a Dispose method, you call that method to clean up any unmanaged resources. Then you get rid of the reference to the object, and it will be garbage collected eventually. You can invoke a garbage collection manually, but there is very rarely any reason to do so. If the system or the .NET heap needs more memory, a garbage collection will occur to try to free up memory. --- 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