-
Notifications
You must be signed in to change notification settings - Fork 6.1k
update foreach for ref variables.
#6202
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
update foreach for ref variables.
#6202
Conversation
|
closing and reopening for a new build. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just one suggested rewording. @BillWagner. Otherwise, this is ready to merge.
|
|
||
| [!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 shows iteration using 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The following example... -> The following example uses a ref iteration variable to set the value...
|
|
||
| [!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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...when the collection type supports
refaccess to its elements...
Should that be clarified? How do I know when the collection type supports ref access to the elements? Is the only sufficient condition is that the Current property returns ref T (or it's just one of several)? If yes, is it fine to rephrase the following way:
Beginning with C# 7.3, when the
Currentproperty returnsref TwhereTis the type of the collection element, you can declare the iteration variable with thereforref readonlymodifier.
I don't like mentioning the Current property here. However, that is introduced anyway at the start of the topic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pkulikov Your proposed change is great. Would you like to submit a PR for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BillWagner I'll create a PR with the change today.
Fixes #5707
Relies on dotnet/samples#146