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. Disposing Collection of IDisposable?

Disposing Collection of IDisposable?

Scheduled Pinned Locked Moved C#
questiondata-structuresdiscussion
2 Posts 2 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.
  • D Offline
    D Offline
    Dr Herbie
    wrote on last edited by
    #1

    Hi folks, Having just discovered the delights of using IDisposable properly, I have a question concerning the disposal of collections of IDisposable objects. The scenario is as follows: I have a class implementing IDisposable. This class owns a collection of other objects that implement IDisposable. So, when I dispose the main object, should I dispose the contents of the array as well? Any advise, comments and/or opinions welcome. PS: Sorry for any typos I suffering mild RSI (three days of furious coding) Dr Herbie @ Autoscribe Remember, half the people out there have below average IQs.

    D 1 Reply Last reply
    0
    • D Dr Herbie

      Hi folks, Having just discovered the delights of using IDisposable properly, I have a question concerning the disposal of collections of IDisposable objects. The scenario is as follows: I have a class implementing IDisposable. This class owns a collection of other objects that implement IDisposable. So, when I dispose the main object, should I dispose the contents of the array as well? Any advise, comments and/or opinions welcome. PS: Sorry for any typos I suffering mild RSI (three days of furious coding) Dr Herbie @ Autoscribe Remember, half the people out there have below average IQs.

      D Offline
      D Offline
      Daniel Turini
      wrote on last edited by
      #2

      If the class "owns" the objects (e.g., private fields) then, yes, you should dispose them. If the class is merely a container for the objects (e.g., a collection like ArrayList), then no, the class user is responsible for that. Due to technical difficulties my previous signature, "I see dumb people" will be off until further notice. Too many people were thinking I was talking about them... :sigh:

      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