Discussion: Using Statement And IDisposable #2948
Replies: 8 comments
-
Disagree The primary use is not one and only of The intention of It just being The primary use. Secondary is everything else we would wanted to |
Beta Was this translation helpful? Give feedback.
-
Right, I agree the wording does indicate there are other valid uses, but If that is the official stance on the interface then the documentation should be updated to make that more clear. |
Beta Was this translation helpful? Give feedback.
-
@Thaina I mean this direct quote "You should implement IDisposable only if your type uses unmanaged resources directly." Kind-of indicates otherwise. [Edit] It seems the link brings you to the wrong place on the page sometimes, find the section titled "Implementing IDisposable" |
Beta Was this translation helpful? Give feedback.
-
I'm confused what the ask here is. Are you asking for the documentation on IDisposable to be updated, a new interface to be introduced, or something else? |
Beta Was this translation helpful? Give feedback.
-
@333fred I interpret it as asking for either documentation update reflecting that IDisposable is not just for unmanaged resources anymore, or a new interface that is compatible with using statements that can be used in place of IDisposable when not using unmanaged resources. |
Beta Was this translation helpful? Give feedback.
-
@333fred A third, though less desirable, option would be that the documentation for IDisposable is updated so that it's explicitly clear that it's only for unmanaged resources and to use the try {} finally {} pattern for classes working with managed resources that you want to "Dispose" of. |
Beta Was this translation helpful? Give feedback.
-
People already use IDisposable for purposes other than unmanaged resources. Introducing a new interface here just means that people are going to use both IDisposable and the new interface, creating a mess. Let's just update the docs to reflect what people actually use it for. Let's not also forget that the "modern" (post-.NET 2.0) recommended way of handling unmanaged resources is using a Consumers of a class - i.e. the code which has to use a |
Beta Was this translation helpful? Give feedback.
-
I have created proposal related to this discussion. See, #3025 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
using
IDisposable
The current implementation of the using statement and accompanying documentation can lead to conflicting opinions on what is proper usage and what is a misuse.
At its core, using(IDisposable) is a shorthand of the try { } finally { Dispose(); } pattern that was common prior to its introduction. The intention of the IDisposable interface is to release unmanaged resources. In practice, the using statement is often used as a scope-based callback. As a result, the IDisposable interface is often used in classes which are not dealing with unmanaged resources which can be considered a misuse of the interface.
There's evidence to support that this was intentional back when the using statement was introduced: (Using for purposes other than disposable objects…) and there is even examples of such within Microsoft's own code: MvcForm.cs.
I'm of the belief that this is not a misuse, but ideally there would be another interface that could be used within a using statement or a mechanism for defining a callback function when the using statement's scope ends. At minimum, I would like to see clarification on the IDisposable's documentation.
Beta Was this translation helpful? Give feedback.
All reactions