Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 7, 2025

This PR creates detailed documentation for CS1622 compiler error to address the misleading information identified in the issue. The original documentation suggested that iterator methods cannot use return statements at all, which is incorrect.

What was fixed

The CS1622 error documentation was incomplete and misleading. The current mention in iterator-yield.md states that iterator methods "can't also use a return statement to return a sequence," but this oversimplifies the actual rules and could confuse developers.

What this PR adds

Creates a new cs1622.md file that provides comprehensive coverage of:

  1. Clarified rules: You cannot mix yield return with return statements in the same iterator method, but you CAN use a single return statement to return an object that implements the required interface (when not using any yield statements).

  2. Clear examples showing both scenarios:

    // This causes CS1622 - mixing yield and return
    public static IEnumerable<int> Invalid()
    {
        yield return 1;
        return new[] { 2, 3 }; // CS1622
    }
    
    // This is valid - return without yield
    public static IEnumerable<int> Valid()
    {
        return new[] { 1, 2, 3 }; // OK
    }
  3. IAsyncEnumerable coverage: The same rules apply to async iterator methods, which was requested in the issue but not documented anywhere.

  4. Multiple fix approaches: Practical solutions including using only yield statements, only return statements, or splitting into separate methods.

The documentation has been validated through compilation testing to ensure accuracy, and passes all markdown linting requirements.

Fixes #40067.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.


Internal previews

📄 File 🔗 Preview link
docs/csharp/misc/cs1622.md docs/csharp/misc/cs1622

Copilot AI changed the title [WIP] This needs more details on the reason for the error Add comprehensive CS1622 compiler error documentation Aug 7, 2025
Copilot AI requested a review from BillWagner August 7, 2025 19:39
@BillWagner
Copy link
Member

closing, as this was fixed in a previous commit.

@BillWagner BillWagner closed this Aug 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

This needs more details on the reason for the error

2 participants