[Proposal]: Introduce a disposable
Keyword in C#
#8691
Replies: 5 comments 12 replies
-
Sounds like a good use case for a custom attribute and an analyzer. |
Beta Was this translation helpful? Give feedback.
-
This is making the assumption that any disposable object that's stored in a local variable must be disposed by the method making that declaration. This is obviously false. It's extremely common to pass around disposable objects like connections, caches, and similar, for consumption by other code that has no part to play in the lifetime of the object. You're also talking about making a breaking change that would impact literally billions of lines of existing code. |
Beta Was this translation helpful? Give feedback.
-
If this is simply about a particular API/pattern wanting additional diagnostics, then teh team position is that this is exactly the space for custom analyzers. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Seems like this proposal is a limited version of general ownership model. I dont think doing aomething like this is feasible before doing a genral ownership feature exploration. Simply labeling field as disposable doesn't save you in a case of multithreaded ownership and and potential problems that go with it. I don't see any mention of use after free (dispose) analysis and how this feature interacts with it. I dont not see any mention of async disposal too. Even assuming this feature is about forcing disposal in a very small context It is not clear how compiler should be able to enforce that if face of exceptions. This rather feels like a hint that "hey, please dispose this after youre done" which is in fact a case for an analyzer. I don't see how this limited feature can really help especially when we might have roles/shapes/extensions (whatever they are called now) like |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Proposal: Introduce a
disposable
Keyword in C#Summary
This proposal introduces a
disposable
keyword in C# to explicitly mark objects that implementIDisposable
at the point of declaration. The goal is to enhance code clarity and prevent unnoticed misuse of disposable objects.Motivation
In the current state of C#, any object can implement
IDisposable
, but this is not apparent to developers unless they inspect the source code or documentation. This ambiguity leads to several issues:using
statement or manually disposed.This proposal aims to address these issues by requiring developers to explicitly acknowledge disposability at the point of declaration using a
disposable
keyword. This is similar in philosophy toref
andout
at call site, which enforce clarity and intent.Design Proposal
1.
disposable
Keyword for Local VariablesThe
disposable
keyword is used when declaring local variables that implementIDisposable
. This ensures developers are immediately aware that the object requires cleanup.Example:
disposable
keyword, the compiler raises an error.using
, explicitDispose
, etc.).2.
disposable
for Fields and PropertiesFields and properties holding disposable objects must also use the
disposable
keyword. This explicitly indicates that the object implementsIDisposable
and highlights the need for proper resource management, making its lifecycle requirements clear.Example:
_connection
is a disposable object and that developers must take responsibility for ensuring it is properly disposed of during the object's lifecycle.3. Integration with
using
The
using
keyword already implies disposable management. No changes are required forusing
declarations or blocks, asusing
already enforces disposal.Example:
Key Benefits
IDisposable
.Opt-In or Automatic
The
disposable
keyword could be:<EnableDisposableEnforcement>true</EnableDisposableEnforcement>
.Comparison to Current State
disposable
)Conclusion
A
disposable
keyword provides an elegant solution to a longstanding issue withIDisposable
handling in C#. By making disposability explicit and enforceable, it improves code safety, clarity, and developer productivity. This proposal aligns with C#'s focus on modern, safe, and clear language design and deserves serious consideration.Beta Was this translation helpful? Give feedback.
All reactions