Proposal: Allow `using` statement to structurally match `IDisposable` on ref structs (16.3, Core 3)
- Dominant language
- C#
- Stars
- 12.7k
- Forks
- 1.1k
- Avg merge
- 11h 1m
- Merged PRs (30d)
- 3
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
* [x] Proposed
* [x] Discussed in LDM
* [x] Prototype: [done](https://github.com/dotnet/roslyn/pull/28315)
* [x] Implementation: Done
* [x] Specification: #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)
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.