Skip to content

[Proposal]: Collection Expressions in foreach #9739

@CyrusNajmabadi

Description

@CyrusNajmabadi

Collection Expressions in foreach

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);
}

Design meetings

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions