Skip to content

Commit

Permalink
Proofread
Browse files Browse the repository at this point in the history
  • Loading branch information
BillWagner committed Jul 1, 2024
1 parent c4a5471 commit 154ac89
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 51 deletions.
12 changes: 6 additions & 6 deletions docs/csharp/language-reference/builtin-types/ref-struct.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ You can use the `ref` modifier in the declaration of a [structure type](struct.m
- A `ref struct` can't implement interfaces.
- A `ref struct` can't be boxed to <xref:System.ValueType?displayProperty=nameWithType> or <xref:System.Object?displayProperty=nameWithType>.
- A `ref struct` can't be a type argument.
- A `ref struct` variable can't be captured by a [lambda expression](../operators/lambda-expressions.md) or a [local function](../../programming-guide/classes-and-structs/local-functions.md).
- A `ref struct` variable can't be used in the same block as the [`await`](../operators/await.md) expression in an [`async`](../keywords/async.md) method. Prior to C# 13, `ref struct` variables can't be used in an `async` method. However, you can use `ref struct` variables in synchronous methods, for example, in methods that return <xref:System.Threading.Tasks.Task> or <xref:System.Threading.Tasks.Task%601>.
- Prior to C# 13, a `ref struct` variable can't be used in [iterators](../../iterators.md). Beginning with C# 13, `ref struct` types and `ref` locals may be used in iterators, provided they are not in code segments with the `yield return` statement.
- A `ref struct` variable can't be captured in a [lambda expression](../operators/lambda-expressions.md) or a [local function](../../programming-guide/classes-and-structs/local-functions.md).
- A `ref struct` variable can't be used in the same block as the [`await`](../operators/await.md) expression in an [`async`](../keywords/async.md) method. Before C# 13, `ref struct` variables can't be used in an `async` method. However, you can use `ref struct` variables in synchronous methods, for example, in methods that return <xref:System.Threading.Tasks.Task> or <xref:System.Threading.Tasks.Task%601>.
- Before C# 13, a `ref struct` variable can't be used in [iterators](../../iterators.md). Beginning with C# 13, `ref struct` types and `ref` locals can be used in iterators, provided they aren't in code segments with the `yield return` statement.

You can define a disposable `ref struct`. To do that, ensure that a `ref struct` fits the [disposable pattern](~/_csharplang/proposals/csharp-8.0/using.md#pattern-based-using). That is, it has an instance `Dispose` method, which is accessible, parameterless and has a `void` return type. You can use the [using statement or declaration](../statements/using.md) with an instance of a disposable `ref struct`.

Expand All @@ -34,13 +34,13 @@ Beginning with C# 11, you can declare a `ref` field in a `ref struct`, as the fo

:::code language="csharp" source="snippets/shared/StructType.cs" id="SnippetRefField":::

A `ref` field may have the `null` value. Use the <xref:System.Runtime.CompilerServices.Unsafe.IsNullRef%60%601(%60%600@)?displayProperty=nameWithType> method to determine if a `ref` field is `null`.
A `ref` field can have the `null` value. Use the <xref:System.Runtime.CompilerServices.Unsafe.IsNullRef%60%601(%60%600@)?displayProperty=nameWithType> method to determine if a `ref` field is `null`.

You can apply the `readonly` modifier to a `ref` field in the following ways:

- `readonly ref`: You can [ref reassign](../operators/assignment-operator.md#ref-assignment) such a field with the `= ref` operator only inside a constructor or an [`init` accessor](../keywords/init.md). You can assign a value with the `=` operator at any point allowed by the field access modifier.
- `ref readonly`: At any point, you cannot assign a value with the `=` operator to such a field. However, you can ref reassign a field with the `= ref` operator.
- `readonly ref readonly`: You can only ref reassign such a field in a constructor or an `init` accessor. At any point, you cannot assign a value to the field.
- `ref readonly`: At any point, you can't assign a value with the `=` operator to such a field. However, you can ref reassign a field with the `= ref` operator.
- `readonly ref readonly`: You can only ref reassign such a field in a constructor or an `init` accessor. At any point, you can't assign a value to the field.

The compiler ensures that a reference stored in a `ref` field doesn't outlive its referent.

Expand Down
6 changes: 3 additions & 3 deletions docs/csharp/language-reference/compiler-messages/cs1996.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Cannot await in the body of a lock statement

## Example

The following sample generates CS1996:
The following sample generates CS1996:

```csharp
public class C
Expand All @@ -33,11 +33,11 @@ public class C
}
```

This code still generates this error with C# 13, as the `await` is in the `lock` statement block.
The preceding code produces the same error with C# 13, as the `await` is in the `lock` statement block.

## To correct this error

Asynchronous code within a `lock` statement block is hard to implement reliably and even harder to implement in a general sense. The C# compiler doesn't support doing this to avoid emitting code that will be prone to deadlocks. Extracting the asynchronous code from the `lock` statement block will correct this error. For example:
Asynchronous code within a `lock` statement block is hard to implement reliably and even harder to implement in a general sense. The C# compiler doesn't support doing this to avoid emitting code prone to deadlocks. Extracting the asynchronous code from the `lock` statement block corrects this error. For example:

```csharp
public class C
Expand Down
6 changes: 3 additions & 3 deletions docs/csharp/language-reference/compiler-messages/cs4004.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
description: "Compiler Error CS4004"
title: "Compiler Error CS4004"
ms.date: 9/12/2022
ms.date: 07/01/20242
f1_keywords:
- "CS4004"
helpviewer_keywords:
Expand All @@ -13,7 +13,7 @@ Cannot await in an unsafe context

## Example

The following sample generates CS4004:
The following sample generates CS4004:

```csharp
using System.Threading.Tasks;
Expand Down Expand Up @@ -53,7 +53,7 @@ The `ReverseText` method naively uses a background task to asynchronously create

## To correct this error

Separating the unsafe code from the awaitable code will correct this error. One separation technique is creating a new method for the unsafe code and then calling it from the awaitable code. For example:
Separating the unsafe code from the awaitable code corrects this error. One separation technique is creating a new method for the unsafe code and then calling it from the awaitable code. For example:

```csharp
public static class C
Expand Down
6 changes: 3 additions & 3 deletions docs/csharp/language-reference/compiler-messages/cs4013.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ helpviewer_keywords:

Instance of type cannot be used inside a nested function, query expression, iterator block or async method

Beginning with C# 13, `ref struct` types can be used in iterator methods, provided that they are not in scope during a `yield return` statement.
Beginning with C# 13, `ref struct` types can be used in iterator methods, if they aren't accessed across `yield return` statement.

## Example

Expand Down Expand Up @@ -40,7 +40,7 @@ This enumerator method extracts lines of text from a character array. It naivel

## To correct this error

`Lines(char[] text)` is an enumerator function. An enumerator function compiles the method's body into a state machine that manages the sequence of states the iterator function goes through while processing. That state machine is implemented as a generated class, and the state is implemented as variables within that class. That captured local state is forced from a stack context to a heap context. Since `ref struct`s like `ReadOnlySpan<T>` cannot be stored in the heap, the CS4013 error is raised. To continue to use a `ReadOnlySpan<T>`, to correct this error, the method must be re-implemented as a non-iterator function, for example:
`Lines(char[] text)` is an enumerator function. An enumerator function compiles the method's body into a state machine that manages the sequence of states the iterator function goes through while processing. That state machine is implemented as a generated class, and the state is implemented as variables within that class. That captured local state is forced from a stack context to a heap context. Since `ref struct`s like `ReadOnlySpan<T>` can't be stored in the heap, the CS4013 error is raised. To continue to use a `ReadOnlySpan<T>`, to correct this error, the method must be reimplemented as a noniterator function, for example:

```csharp
public static IEnumerable<string> Lines2(char[] text)
Expand All @@ -61,7 +61,7 @@ This enumerator method extracts lines of text from a character array. It naivel
}
```

To continue to use an iterator function, to correct this error, the method must be re-implemented to avoid using `ReadOnlySpan<T>`, for example:
To continue to use an iterator function, to correct this error, the method must be reimplemented to avoid using `ReadOnlySpan<T>`, for example:

```csharp
public static IEnumerable<string> Lines2(char[] chars)
Expand Down
2 changes: 1 addition & 1 deletion docs/csharp/language-reference/compiler-messages/cs8176.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ helpviewer_keywords:

Iterators cannot have by-reference locals

Iterator blocks use deferred execution, where the evaluation of an expression is delayed until its realized value is actually required. To manage that deferred execution state, iterator blocks use a state machine, capturing variable state in closures implemented in compiler-generated classes and properties. A local variable reference (on the stack) cannot be captured within the instance of a class in the heap, so the compiler issues an error.
Iterator blocks use deferred execution, where the evaluation of an expression is delayed until its realized value is required. To manage that deferred execution state, iterator blocks use a state machine, capturing variable state in closures implemented in compiler-generated classes and properties. A local variable reference (on the stack) can't be captured within the instance of a class in the heap, so the compiler issues an error.

Beginning with C# 13, this restriction was removed.

Expand Down
4 changes: 2 additions & 2 deletions docs/csharp/language-reference/compiler-messages/cs8177.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ helpviewer_keywords:

Async methods cannot have by-reference locals

To manage asynchronous state, `async` methods use a state machine, capturing variable state in closures implemented in compiler-generated classes and properties. A local variable reference (on the stack) cannot be captured within the instance of a class in the heap, so the compiler issues an error.
To manage asynchronous state, `async` methods use a state machine, capturing variable state in closures implemented in compiler-generated classes and properties. A local variable reference (on the stack) can't be captured within the instance of a class in the heap, so the compiler issues an error.

## Example

The following sample generates CS8177 in C# prior to C# 13:
The following sample generates CS8177 before C# 13:

```csharp
// CS8177.cs (20,26)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "Compiler warning waves"
description: "C# warning waves are optional warnings that can be reported on code where previously a warning wouldn't have been reported. They represent practices that could be harmful, or potentially elements that may be breaking changes in the future."
description: "C# warning waves are optional warnings that can be reported on code where previously a warning wouldn't have been reported. They represent practices that could be harmful, or potentially elements that might be breaking changes in the future."
ms.date: 07/01/2024
f1_keywords:
- "CS7023"
Expand Down Expand Up @@ -39,7 +39,7 @@ helpviewer_keywords:
---
# C# Warning waves

New warnings and errors may be introduced in each release of the C# compiler. When new warnings could be reported on existing code, those warnings are introduced under an opt-in system referred to as a *warning wave*. The opt-in system means that you shouldn't see new warnings on existing code without taking action to enable them. Warning waves are enabled using the [**AnalysisLevel**](../compiler-options/errors-warnings.md#analysis-level) element in your project file. When `<TreatWarningsAsErrors>true</TreatWarningsAsErrors>` is specified, enabled warning wave warnings generate errors. Warning wave 5 diagnostics were added in C# 9. Warning wave 6 diagnostics were added in C# 10. Warning wave 7 diagnostics were added in C# 11. Warning wave 8 diagnostics were added in C# 12.
New warnings and errors can be introduced in each release of the C# compiler. When new warnings could be reported on existing code, those warnings are introduced under an opt-in system referred to as a *warning wave*. The opt-in system means that you shouldn't see new warnings on existing code without taking action to enable them. Warning waves are enabled using the [**AnalysisLevel**](../compiler-options/errors-warnings.md#analysis-level) element in your project file. When `<TreatWarningsAsErrors>true</TreatWarningsAsErrors>` is specified, enabled warning wave warnings generate errors. Warning wave 5 diagnostics were added in C# 9. Warning wave 6 diagnostics were added in C# 10. Warning wave 7 diagnostics were added in C# 11. Warning wave 8 diagnostics were added in C# 12.

## CS9123 - Taking address of local or parameter in async method can create a GC hole.

Expand All @@ -66,7 +66,7 @@ You can address this warning by renaming the type to include at least one non-lo

*Warning wave 6*

This warning corrects some inconsistencies in reporting differences between partial method signatures. The compiler always reported an error when the partial method signatures created different CLR signatures. Now, the compiler reports CS8826 when the signatures are syntactically different C#. Consider the following partial class:
This warning corrects some inconsistencies in reporting differences between partial method signatures. The compiler always reported an error when the partial method signatures created different CLR signatures. Now, the compiler reports CS8826 when the signatures are syntactically different C#. Consider the following partial class:

:::code language="csharp" source="./snippets/WarningWaves/WaveSix.cs" id="PartialMethodDeclaration":::

Expand All @@ -87,7 +87,7 @@ The `is` and `as` expressions always return `false` for a static type because yo

:::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="StaticTypeAsIs":::

The compiler reports this warning because the type test can never succeed. To correct this warning, remove the test and remove any code executed only if the test succeeded. In the preceding example, the `else` clause is always executed. The method body could be replaced by that single line:
The compiler reports this warning because the type test can never succeed. To correct this warning, remove the test and remove any code executed only if the test succeeded. In the preceding example, the `else` clause is always executed. You can replace that method body with that single line:

```csharp
Console.WriteLine("o is not an instance of a static class");
Expand All @@ -97,7 +97,7 @@ Console.WriteLine("o is not an instance of a static class");

*Warning wave 5*

The `==` and `!=` operators always return `false` (or `true`) when comparing an instance of a `struct` type to `null`. The following code demonstrates this warning. Assume `S` is a `struct` that has defined `operator ==` and `operator !=`:
The `==` and `!=` operators always return `false` (or `true`) when comparing an instance of a `struct` type to `null`. The following code demonstrates this warning. Assume `S` is a `struct` that defines `operator ==` and `operator !=`:

:::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="StructsArentNull":::

Expand All @@ -115,7 +115,7 @@ To fix this error, put parentheses around the query expression:

:::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="QueryPrecedenceNoWarn":::

## Members must be fully assigned, use of unassigned variable (CS8880, CS8881, CS8882, CS8883, CS8884, CS8885, CS8886, CS8887)
## Members must be fully assigned. Use of unassigned variable (CS8880, CS8881, CS8882, CS8883, CS8884, CS8885, CS8886, CS8887)

*Warning wave 5*

Expand Down
6 changes: 3 additions & 3 deletions docs/csharp/language-reference/statements/yield.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ You use the `yield` statement in an [iterator](../../iterators.md) to provide th

Iteration also finishes when control reaches the end of an iterator.

In the preceding examples, the return type of iterators is <xref:System.Collections.Generic.IEnumerable%601> (in non-generic cases, use <xref:System.Collections.IEnumerable> as the return type of an iterator). You can also use <xref:System.Collections.Generic.IAsyncEnumerable%601> as the return type of an iterator. That makes an iterator async. Use the [`await foreach` statement](iteration-statements.md#await-foreach) to iterate over iterator's result, as the following example shows:
In the preceding examples, the return type of iterators is <xref:System.Collections.Generic.IEnumerable%601> (in nongeneric cases, use <xref:System.Collections.IEnumerable> as the return type of an iterator). You can also use <xref:System.Collections.Generic.IAsyncEnumerable%601> as the return type of an iterator. That makes an iterator async. Use the [`await foreach` statement](iteration-statements.md#await-foreach) to iterate over iterator's result, as the following example shows:

:::code language="csharp" source="snippets/yield/Program.cs" id="IteratorAsync":::

<xref:System.Collections.Generic.IEnumerator%601> or <xref:System.Collections.IEnumerator> can also be the return type of an iterator. That is useful when you implement the `GetEnumerator` method in the following scenarios:
<xref:System.Collections.Generic.IEnumerator%601> or <xref:System.Collections.IEnumerator> can also be the return type of an iterator. Use those return types when you implement the `GetEnumerator` method in the following scenarios:

- You design the type that implements <xref:System.Collections.Generic.IEnumerable%601> or <xref:System.Collections.IEnumerable> interface.
- You add an instance or [extension](../../programming-guide/classes-and-structs/extension-methods.md) `GetEnumerator` method to enable iteration over the type's instance with the [`foreach` statement](iteration-statements.md#the-foreach-statement), as the following example shows:
Expand All @@ -37,7 +37,7 @@ You can't use the `yield` statements in:

- methods with [in](../keywords/method-parameters.md#in-parameter-modifier), [ref](../keywords/ref.md), or [out](../keywords/method-parameters.md#out-parameter-modifier) parameters
- [lambda expressions](../operators/lambda-expressions.md) and [anonymous methods](../operators/delegate-operator.md)
- [unsafe blocks](../keywords/unsafe.md). Prior to 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.
- [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.

## Execution of an iterator

Expand Down
39 changes: 18 additions & 21 deletions docs/csharp/misc/cs1629.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,28 @@ f1_keywords:
- "CS1629"
helpviewer_keywords:
- "CS1629"
ms.assetid: 907eae46-0265-4cd0-b27b-ff555d004259
---
# Compiler Error CS1629

Unsafe code may not appear in iterators
Unsafe code may not appear in iterators

This restriction is relaxed in C# 13. You can use `unsafe` blocks, but the `yield return` statement can't be used in an `unsafe` block.

The C# language specification does not allow unsafe code in iterators.

The following sample generates CS1629:

```csharp
// CS1629.cs
The C# language specification doesn't allow unsafe code in iterators. This restriction is relaxed in C# 13. You can use `unsafe` blocks, but the `yield return` statement can't be used in an `unsafe` block.

The following sample generates CS1629:

```csharp
// CS1629.cs
// compile with: /unsafe
using System.Collections.Generic;
using System.Collections.Generic;
class C
{
IEnumerator<int> IteratorMeth() {
int i;
unsafe // CS1629
{
int *p = &i;
yield return *p;
}
}
}
{
IEnumerator<int> IteratorMeth() {
int i;
unsafe // CS1629
{
int *p = &i;
yield return *p;
}
}
}
```
Loading

0 comments on commit 154ac89

Please sign in to comment.