Skip to content

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.


Internal previews

📄 File 🔗 Preview link
docs/csharp/language-reference/statements/yield.md yield statement - provide the next element

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
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ready for final review.

@BillWagner BillWagner marked this pull request as ready for review August 25, 2025 15:57
@BillWagner BillWagner requested a review from a team as a code owner August 25, 2025 15:57
@BillWagner BillWagner requested a review from adegeo August 25, 2025 15:57
Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com>
@BillWagner BillWagner enabled auto-merge (squash) August 25, 2025 19:17
@BillWagner BillWagner merged commit c1e8fbe into main Aug 25, 2025
10 checks passed
@BillWagner BillWagner deleted the copilot/fix-45331 branch August 25, 2025 19:20
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
3 participants