Skip to content

Add documentation for using statements in iterators #48071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 22, 2025

Addresses confusion about the relationship between try blocks and using statements in iterator methods by adding clear documentation and a working example.

Problem

Anonymous feedback indicated confusion about whether using blocks can be used inside iterator functions and whether they get auto-disposed after the first yield return. This is a common source of confusion since iterators have restrictions on try blocks with catch clauses.

Solution

Added a new section "Using statements in iterators" to the yield statement documentation that explains:

  1. using statements work correctly in iterators because they compile to try/finally blocks without catch clauses
  2. Resources persist throughout iterator execution - they are NOT disposed after the first yield return
  3. Proper disposal occurs when the iterator completes or when the iterator itself is disposed (e.g., when caller breaks early)

Code Example

The documentation includes a practical example demonstrating:

static IEnumerable<string> ReadLinesFromResource()
{
    Console.WriteLine("Opening resource...");
    using var resource = new StringWriter(); // Resource managed by using statement
    resource.WriteLine("Resource initialized");
    
    string[] lines = { "Line 1", "Line 2", "Line 3", "Line 4" };
    
    foreach (string line in lines)
    {
        Console.WriteLine($"About to yield: {line}");
        yield return line; // Resource remains available across yields
        Console.WriteLine($"Resumed after yielding: {line}");
    }
    
    Console.WriteLine("Iterator completed - using block will dispose resource.");
}

This example shows that even when iteration breaks early, the resource is properly disposed when the iterator is cleaned up.

Technical Details

The documentation explains that this works because the C# compiler transforms using statements into try/finally blocks without catch clauses, which are explicitly permitted in iterator methods (unlike try blocks with catch clauses).

Fixes #45331.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Explain the relationship between try blocks and using for iterators Add documentation for using statements in iterators Aug 22, 2025
@Copilot Copilot AI requested a review from BillWagner August 22, 2025 18:21
Copilot finished work on behalf of BillWagner August 22, 2025 18:21
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.

Explain the relationship between try blocks and using for iterators
2 participants