Skip to content

Commit 028fdf0

Browse files
pkulikovBillWagner
authored andcommitted
Clarified when foreach can be used with ref (#6225)
1 parent e04bd64 commit 028fdf0

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

docs/csharp/language-reference/keywords/foreach-in.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "foreach, in (C# Reference)"
3-
ms.date: 06/28/2018
3+
ms.date: 06/29/2018
44
f1_keywords:
55
- "foreach"
66
- "foreach_CSharpKeyword"
@@ -29,11 +29,11 @@ The following example shows usage of the `foreach` statement with an instance of
2929

3030
The next example uses the `foreach` statement with an instance of the <xref:System.Span%601?displayProperty=nameWithType> type, which doesn't implement any interfaces:
3131

32-
[!code-csharp-interactive[span example](~/samples/snippets/csharp/keywords/IterationKeywordsExamples.cs#2)]
32+
[!code-csharp[span example](~/samples/snippets/csharp/keywords/IterationKeywordsExamples.cs#2)]
3333

34-
Beginning with C# 7.3, when the collection type supports `ref` access to its elements, you can declare the iteration variable with the `ref` or `ref readonly` modifier. The following example uses a `ref` iteration variable to set the value of each item in a stackalloc array. The `ref readonly` version iterates the collection to print all the values. The `readonly` declaration uses an implicit local variable declaration. Implicit variable declarations can be used with either `ref` or `ref readonly` declarations, as can explicitly typed variable declarations.
34+
Beginning with C# 7.3, if the enumerator's `Current` property returns a [reference return value](../../programming-guide/classes-and-structs/ref-returns.md) (`ref T` where `T` is the type of the collection element), you can declare the iteration variable with the `ref` or `ref readonly` modifier. The following example uses a `ref` iteration variable to set the value of each item in a stackalloc array. The `ref readonly` version iterates the collection to print all the values. The `readonly` declaration uses an implicit local variable declaration. Implicit variable declarations can be used with either `ref` or `ref readonly` declarations, as can explicitly typed variable declarations.
3535

36-
[!code-csharp-interactive[ref span example](~/samples/snippets/csharp/keywords/IterationKeywordsExamples.cs#RefSpan)]
36+
[!code-csharp[ref span example](~/samples/snippets/csharp/keywords/IterationKeywordsExamples.cs#RefSpan)]
3737

3838
## C# language specification
3939

@@ -47,4 +47,4 @@ Beginning with C# 7.3, when the collection type supports `ref` access to its ele
4747
[Iteration Statements](iteration-statements.md)
4848
[C# Keywords](index.md)
4949
[C# Reference](../index.md)
50-
[C# Programming Guide](../../programming-guide/index.md)
50+
[C# Programming Guide](../../programming-guide/index.md)

0 commit comments

Comments
 (0)