using declaration for foreach
#9706
Unanswered
ahdung
asked this question in
Language Ideas
Replies: 3 comments 2 replies
-
You can do: foreach (var reader in GetReaders()) using (reader)
{
// use reader
} |
Beta Was this translation helpful? Give feedback.
2 replies
-
Beta Was this translation helpful? Give feedback.
0 replies
-
You can also avoid the identation with a placeholder variable. The foreach iteration variable can't be reassigned so this shouldn't create confusion as to what is getting disposed. foreach(var reader in GetReaders())
{
using var _ = reader;
// use reader
} |
Beta Was this translation helpful? Give feedback.
0 replies
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.
-
I hope can do this:
instead of:
Beta Was this translation helpful? Give feedback.
All reactions