Skip to content

Commit

Permalink
update foreach for ref variables. (dotnet#6202)
Browse files Browse the repository at this point in the history
* update foreach for `ref` variables.

* respond to feedback.
  • Loading branch information
BillWagner authored Jun 28, 2018
1 parent 848f117 commit 3b153af
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 5 additions & 1 deletion docs/csharp/language-reference/keywords/foreach-in.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "foreach, in (C# Reference)"
ms.date: 05/24/2018
ms.date: 06/28/2018
f1_keywords:
- "foreach"
- "foreach_CSharpKeyword"
Expand Down Expand Up @@ -31,6 +31,10 @@ The next example uses the `foreach` statement with an instance of the <xref:Syst

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

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.

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

## C# language specification

[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)]
Expand Down
2 changes: 1 addition & 1 deletion docs/csharp/whats-new/csharp-7-3.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ ref VeryLargeStruct refLocal = ref veryLargeStruct; // initialization
refLocal = ref anotherVeryLargeStruct; // reassigned, refLocal refers to different storage.
```

For more information, see the article on [`ref` returns and `ref` locals](../programming-guide/classes-and-structs/ref-returns.md).
For more information, see the article on [`ref` returns and `ref` locals](../programming-guide/classes-and-structs/ref-returns.md), and the article on [`foreach`](../language-reference/keywords/foreach-in.md).

### `stackalloc` arrays support initializers

Expand Down

0 comments on commit 3b153af

Please sign in to comment.