Extension GetEnumerator support in foreach - C# feature specifications (original) (raw)

Note

This article is a feature specification. The specification serves as the design document for the feature. It includes proposed specification changes, along with information needed during the design and development of the feature. These articles are published until the proposed spec changes are finalized and incorporated in the current ECMA specification.

There may be some discrepancies between the feature specification and the completed implementation. Those differences are captured in the pertinent language design meeting (LDM) notes.

You can learn more about the process for adopting feature speclets into the C# language standard in the article on the specifications.

Champion issue: https://github.com/dotnet/csharplang/issues/3194

Summary

Allow foreach loops to recognize an extension method GetEnumerator method that otherwise satisfies the foreach pattern, and loop over the expression when it would otherwise be an error.

Motivation

This will bring foreach inline with how other features in C# are implemented, including async and pattern-based deconstruction.

Detailed design

The spec change is relatively straightforward. We modify The foreach statement §13.9.5 section to this text:

The compile-time processing of a foreach statement first determines the collection type, enumerator type and element type of the expression. This determination proceeds as follows:

For await foreach, the rules are similarly modified. The only change that is required to that spec is removing the Extension methods do not contribute. line from the description, as the rest of that spec is based on the above rules with different names substituted for the pattern methods.

Drawbacks

Every change adds additional complexity to the language, and this potentially allows things that weren't designed to be foreached to be foreached, like Range.

Alternatives

Doing nothing.

Unresolved questions

None at this point.