-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Description
Currently the language states that any type used in an using statement must have an implicit conversion to IDisposable. This proposal is allow any type which structurally matches IDisposable (has a public void-returning non-generic instance method taking no arguments) to be used in a using statement.
The semantics of this construct are the same as a regular using, except that if the type does not have an implicit conversion to IDisposable, there is a check to see if it structurally matches IDisposable. If so, instead of the IDisposable.Dispose call, there is a call to the structural Dispose method.
At the moment, extension methods are not considered, like foreach statements. If we were to add extension method support to GetEnumerator for foreach, I propose we also add it for using.
[jcouv update:] This feature was restricted to ref structs due to backwards compatibility reasons. Also, the part about considering extension methods was split out of this feature, and it was not implemented in dev16.0 milestone.
Full proposal in https://github.com/dotnet/csharplang/blob/master/proposals/csharp-8.0/using.md
- Proposed
- Discussed in LDM
- Prototype: done
- Implementation: Done
- Specification: Using patterns and declarations #1703
LDM history:
- https://github.com/dotnet/csharplang/blob/master/meetings/2018/LDM-2018-07-09.md
- https://github.com/dotnet/csharplang/blob/master/meetings/2018/LDM-2018-12-03.md#using-declaration-open-issues
- https://github.com/dotnet/csharplang/blob/master/meetings/2018/LDM-2018-12-12.md#when-we-start-recognizing-pattern-dispose-do-we-call-it-in-foreach (pattern-based disposal in foreach)