-3

What keywords implement or will call dispose() in the background in C#? When is it necessary to explicitly call dispose on versus using IDispose?

  • I'm afraid I have no idea what you're talking about. I think you may be confused, could you add more details and maybe and example? – Claudio Redi Jun 02 '15 at 20:46
  • Does `DisposableObject.Dispose()` not help you? – Der Kommissar Jun 02 '15 at 20:47
  • 1
    Do you mean like the way a `using` block implicitly disposes at the end? – Arin Jun 02 '15 at 20:48
  • Check these two answers on the same question: [here](http://stackoverflow.com/a/2926890/3773066) and [here](http://stackoverflow.com/a/2926928/3773066). Also, read up on the [IDisposable.Dispose Method](https://msdn.microsoft.com/en-us/library/system.idisposable.dispose(v=vs.110).aspx). – OhBeWise Jun 02 '15 at 21:12
  • Firstly, the interface is called IDisposable. Dispose can be implemented by classes which are derived from this interface. The only keyword I know of which is using dispose is "using". Despose might also be called during garbage collection by the destructor but do not rely on this! For more info maybe take a look at the dispose pattern: https://msdn.microsoft.com/en-us/library/b1yfkh5e%28v=vs.110%29.aspx – Manuel Zelenka Jun 02 '15 at 21:23

1 Answers1

1

If an object implements IDispose, then yes, call Dispose on the object.

For example, objects with a using block:

using (var ms = new MemoryStream()) {
  //...
}

Here is some more information on Implementing the Dispose Method

EDIT: Also this old Stack Overflow post is really great: Do you need to dispose of objects and set them to null?

Community
  • 1
  • 1
Kala J
  • 2,040
  • 4
  • 45
  • 85