1

The given answer (B) to the below question seems to contradict the IDisposable documentation which reads:

You should implement IDisposable only if your type uses unmanaged resources directly.

Plus, almost every class I write has managed resources therefore should I be implementing IDisposable in almost every class?

  1. If a class has managed resources and no unmanaged resources, it should do which of the following?

a. Implement IDisposable and provide a destructor.
b. Implement IDisposable and not provide a destructor.
c. Not implement IDisposable and provide a destructor.
d. Not implement IDisposable and not provide a destructor.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
  • If your class has disposable fields, you should implement `IDisposable` to dispose of them in the `Dispose` method. Now following the common disposable pattern if your class is not meant to be inherited, seal it and don't provide a destructor. Otherwise, you should fully implement the disposable pattern (https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose). – vc 74 Oct 24 '20 at 08:59
  • `b.` is correct. The sentence from the documentation is wrong. The correct implementation of `IDisposable` is [here](https://stackoverflow.com/a/898867/11683). – GSerg Oct 24 '20 at 09:09
  • 1
    Might be worth mentioning that the 70-483 certification was deprecated and will soon be removed :) – Camilo Terevinto Oct 24 '20 at 09:09
  • It was to be removed on July 2020 to be exact, but due to the pandemic it will be removed in Jan 31 2021. – Sotiris Panopoulos Oct 24 '20 at 09:28
  • What exactly is the definition of a "managed resource" in the context of this question? – Matthew Watson Oct 24 '20 at 10:43
  • Note that there are other cases where you will need a Dispose method, even if your class doesn't contain any disposable entities. For example, if your class is passed something which has an event which your class subscribes to. If your class doesn't unsubscribe from that event in a Dispose() method (or via some other mechanism), then instances of your class will be kept alive (in memory) as long as the object with the event is alive, because there would be a live reference from that object to the instance of your class. – Matthew Watson Oct 24 '20 at 11:17
  • You read the *wrong* document. The *right* document is the one @vc 74 linked, which comes from this: [The Dispose guidelines really should be updated to point developers in the right direction](https://github.com/dotnet/docs/issues/8463) and then *finalized* :) here: [Sweeping changes to improve dispose guidelines](https://github.com/dotnet/docs/pull/18266). Read the summary in the first link. – Jimi Oct 24 '20 at 12:52

1 Answers1

0

Keyword here is "managed resources", such as a Stream. If you have a field of such a type, it should be disposed when your class is being disposed.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • 4
    Which contradicts to the quote from the documentation (which is wrong (which is what confused the OP)). – GSerg Oct 24 '20 at 09:10
  • Is a `List` a managed resource? How about `int[]`? I find the term "managed resource" to be a little ill-defined. – Matthew Watson Oct 24 '20 at 10:41
  • @MatthewWatson It may be ill-defined in general, but in the context of `IDisposable` it's "what goes into the [`// get rid of managed resources` part](https://stackoverflow.com/a/898867/11683)". – GSerg Oct 24 '20 at 10:46
  • @GSerg Are you defining a "managed resource" as "anything that implements IDisposable"? Because in that case it would be better to just say "IDisposable" I think. – Matthew Watson Oct 24 '20 at 10:53
  • @MatthewWatson Absolutely not. Although in this very narrow context, *yes*, because a "managed resource" in this very narrow context is specifically a shorthand for "anything that implements IDisposable". – GSerg Oct 24 '20 at 10:56