-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Open
Labels
Description
Collection Expressions in foreach
- Specification: https://github.com/dotnet/csharplang/blob/main/proposals/collection-expressions-in-foreach.md
- Discussion: [Proposal Discussion]: Collection Expressions in `foreach` #9740
Summary
Collection Expressions introduced a terse syntax [e1, e2, e3, etc] to create common collection values. This proposal extends their usage to foreach statements, where they can be used directly as the iteration source without requiring an explicit target type, like so:
// Today, developers must write:
foreach (var toggle in new[] { true, false })
{
RunTestWithFeatureFlag(toggle);
}
// With this proposal, they can write:
foreach (var toggle in [true, false])
{
RunTestWithFeatureFlag(toggle);
}