The first sentence in the documentation is this: "Implementing the Dispose method is primarily for releasing unmanaged resources used by your code."
Later, in the The Dispose(bool) method overload section, the documentation states this:
The body of the method consists of two blocks of code:
- A block that frees unmanaged resources.
- A conditional block that frees managed resources. The managed resources that it frees can include: Managed objects that implement IDisposable.
It seems to me that directly using unmanaged resources in C# is rare nowadays, so the primary reason why C# developers implement a Dispose method in their classes is to call .Dispose() on IDisposable objects that their classes have references to.
This would mean that the first sentence in the documentation is incorrect, and that the Dispose method is primarily implemented to release _managed_ resources.
Or am I misunderstanding the terms "managed" and "unmanaged" in this context?
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
Hi 👋 @Bosch-Eli-Black
Thank you so much for posting this issue. We really appreciate it.
You're referring to cascading .Dispose calls, where one of the common examples is to implement IDisposable if you're holding on to any IDisposable impls... right? The thing is, the reason that you do this is so that you can ensure that the unmanaged resources are released/cleaned up. Even if it is indirectly, you're code is still responsible. Does that make sense? I think this is fine the way it is stated now, but we can continue this discussion.
My point is that, even if you're just cascading .Dispose calls - you're still "releasing unmanaged resources used by your code", it's just indirectly.
@IEvangelist Ah, that makes sense, thanks!
Perhaps what tripped me up was the inclusion of "your code" in the first sentence: "Implementing the Dispose method is primarily for releasing unmanaged resources used by your code."
Maybe it would be better if shorten to just this: "Implementing the Dispose method is primarily for releasing unmanaged resources."
Or maybe it's fine as is; your call :)
Most helpful comment
Hi 👋 @Bosch-Eli-Black
Thank you so much for posting this issue. We really appreciate it.
You're referring to cascading
.Disposecalls, where one of the common examples is to implementIDisposableif you're holding on to anyIDisposableimpls... right? The thing is, the reason that you do this is so that you can ensure that the unmanaged resources are released/cleaned up. Even if it is indirectly, you're code is still responsible. Does that make sense? I think this is fine the way it is stated now, but we can continue this discussion.My point is that, even if you're just cascading
.Disposecalls - you're still "releasing unmanaged resources used by your code", it's just indirectly.