Skip to content

Commit c1e8fbe

Browse files
CopilotBillWagnergewarren
authored
Add documentation for using statements in iterators (#48071)
* Initial plan * Add documentation about using statements in iterators with code example Co-authored-by: BillWagner <[email protected]> * Apply suggestions from code review Co-authored-by: Genevieve Warren <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: BillWagner <[email protected]> Co-authored-by: Bill Wagner <[email protected]> Co-authored-by: Genevieve Warren <[email protected]>
1 parent b417a57 commit c1e8fbe

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

docs/csharp/language-reference/statements/snippets/yield/Program.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
GetEnumeratorExample.Example();
55
Console.WriteLine();
66
IteratorExecution();
7+
Console.WriteLine();
8+
UsingInIterator();
79

810
static void YieldReturn()
911
{
@@ -116,3 +118,39 @@ IEnumerable<int> ProduceEvenNumbers(int upto)
116118
// </IteratorExecution>
117119
Console.WriteLine();
118120
}
121+
122+
static void UsingInIterator()
123+
{
124+
// <UsingInIterator>
125+
Console.WriteLine("=== Using in Iterator Example ===");
126+
127+
// Demonstrate that using statements work correctly in iterators
128+
foreach (string line in ReadLinesFromResource())
129+
{
130+
Console.WriteLine($"Read: {line}");
131+
// Simulate processing only first two items
132+
if (line == "Line 2") break;
133+
}
134+
135+
Console.WriteLine("Iteration stopped early - resource should still be disposed.");
136+
137+
static IEnumerable<string> ReadLinesFromResource()
138+
{
139+
Console.WriteLine("Opening resource...");
140+
using var resource = new StringWriter(); // Use StringWriter as a simple IDisposable
141+
resource.WriteLine("Resource initialized");
142+
143+
// These lines would typically come from the resource (e.g., file, database)
144+
string[] lines = { "Line 1", "Line 2", "Line 3", "Line 4" };
145+
146+
foreach (string line in lines)
147+
{
148+
Console.WriteLine($"About to yield: {line}");
149+
yield return line;
150+
Console.WriteLine($"Resumed after yielding: {line}");
151+
}
152+
153+
Console.WriteLine("Iterator completed - using block will dispose resource.");
154+
}
155+
// </UsingInIterator>
156+
}

docs/csharp/language-reference/statements/yield.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ You can't use the `yield` statements in:
4040
- [unsafe blocks](../keywords/unsafe.md). Before C# 13, `yield` was invalid in any method with an `unsafe` block. Beginning with C# 13, you can use `yield` in methods with `unsafe` blocks, but not in the `unsafe` block.
4141
- `yield return` and `yield break` can not be used in [catch](../statements/exception-handling-statements.md) and [finally](../statements/exception-handling-statements.md) blocks, or in [try](../statements/exception-handling-statements.md) blocks with a corresponding `catch` block. The `yield return` and `yield break` statements can be used in a `try` block with no `catch` blocks, only a `finally` block.
4242

43+
## `using` statements in iterators
44+
45+
You can use [`using` statements](using.md) in iterator methods. Since `using` statements are compiled into `try` blocks with `finally` clauses (and no `catch` blocks), they work correctly with iterators. The disposable resources are properly managed throughout the iterator's execution:
46+
47+
:::code language="csharp" interactive="try-dotnet-method" source="snippets/yield/Program.cs" id="UsingInIterator":::
48+
49+
As the preceding example shows, the resource acquired in the `using` statement remains available throughout the iterator's execution, even when the iterator suspends and resumes execution at `yield return` statements. The resource is disposed when the iterator completes (either by reaching the end or via `yield break`) or when the iterator itself is disposed (for example, when the caller breaks out of enumeration early).
50+
4351
## Execution of an iterator
4452

4553
The call of an iterator doesn't execute it immediately, as the following example shows:

0 commit comments

Comments
 (0)