Allow using 'using' in foreach for the enumerable. #7716
Unanswered
Raphtaliyah
asked this question in
Language Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
If an enumerable type implements
IDisposable
(or the dispose pattern for ref structs) and an instance would only be created to be enumerated, currently there is no way to use it in a foreach loop without using a local variable.This proposal is to allow using
using
inside the "header" of theforeach
loop like this:The example is from an ECS where the created
View
holds unmanaged memory and must be disposed, finalizers can't be used because theView
is a ref struct. The current workaround is to have a local variable:The local variable is completely unnecessary unless it's enumerated multiple times, which is rarely the case and requires longer names if multiple
View
s are created in the same scope. Allowingusing
inside theforeach
loop would help clean up this and code similar to this.The lifetime of the enumerable would either match the lifetime of the
View
in the local variable example or would be disposed at the end of theforeach
loop.Beta Was this translation helpful? Give feedback.
All reactions