.net - How to dispose a list of disposable objects? -


I am implementing IDisposable for a square and when I settle an internal list of disposable objects, I will remove them in looping. Public class MyDisposable Implements IDisposable Private _disposbaleObjects Public Disclaimers (Other Dispossibles) Public Disclaimers Public Disclaimers () Implements System.IDisposable.Dispose Dispose (True) GC.SuppressFinalize (m) end Sub-protected overridive sub-disposal (by-deletion as boolean) if not so. Should I settle such a list? For each obj _disposbaleObjects obj.dispose () next end if end if Me.disposedValue = True End Sub End Class

Yes, redistribute each item and every item through the list.

In addition to this, you can type an extension method:

  remove public static zero (this is IEnumerable & lt; IDisposable & gt; ; Collection) {foreach (IDisposable items in the collection) {if (item! = Null) {try {item.Dispose (); } Hold (exception) {// log exception and issue}}}  

and call it for your list

  _disposbaleObjects.Dispose ()  

Comments