Skip to content

Proposal: Allow using statement to structurally match IDisposable on ref structs (16.3, Core 3) #1623

@agocke

Description

@agocke

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

LDM history:

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions