You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/builtin-types/ref-struct.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,9 +12,9 @@ You can use the `ref` modifier in the declaration of a [structure type](struct.m
12
12
- A `ref struct` can't implement interfaces.
13
13
- A `ref struct` can't be boxed to <xref:System.ValueType?displayProperty=nameWithType> or <xref:System.Object?displayProperty=nameWithType>.
14
14
- A `ref struct` can't be a type argument.
15
-
- 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).
16
-
- 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>.
17
-
-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.
15
+
- 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).
16
+
- 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>.
17
+
-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.
18
18
19
19
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`.
20
20
@@ -34,13 +34,13 @@ Beginning with C# 11, you can declare a `ref` field in a `ref struct`, as the fo
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`.
37
+
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`.
38
38
39
39
You can apply the `readonly` modifier to a `ref` field in the following ways:
40
40
41
41
-`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.
42
-
-`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.
43
-
-`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.
42
+
-`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.
43
+
-`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.
44
44
45
45
The compiler ensures that a reference stored in a `ref` field doesn't outlive its referent.
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/compiler-messages/cs1996.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ Cannot await in the body of a lock statement
13
13
14
14
## Example
15
15
16
-
The following sample generates CS1996:
16
+
The following sample generates CS1996:
17
17
18
18
```csharp
19
19
publicclassC
@@ -33,11 +33,11 @@ public class C
33
33
}
34
34
```
35
35
36
-
This code still generates this error with C# 13, as the `await` is in the `lock` statement block.
36
+
The preceding code produces the same error with C# 13, as the `await` is in the `lock` statement block.
37
37
38
38
## To correct this error
39
39
40
-
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:
40
+
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:
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/compiler-messages/cs4004.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
description: "Compiler Error CS4004"
3
3
title: "Compiler Error CS4004"
4
-
ms.date: 9/12/2022
4
+
ms.date: 07/01/20242
5
5
f1_keywords:
6
6
- "CS4004"
7
7
helpviewer_keywords:
@@ -13,7 +13,7 @@ Cannot await in an unsafe context
13
13
14
14
## Example
15
15
16
-
The following sample generates CS4004:
16
+
The following sample generates CS4004:
17
17
18
18
```csharp
19
19
usingSystem.Threading.Tasks;
@@ -53,7 +53,7 @@ The `ReverseText` method naively uses a background task to asynchronously create
53
53
54
54
## To correct this error
55
55
56
-
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:
56
+
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:
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/compiler-messages/cs4013.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,7 @@ helpviewer_keywords:
11
11
12
12
Instance of type cannot be used inside a nested function, query expression, iterator block or async method
13
13
14
-
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.
14
+
Beginning with C# 13, `ref struct` types can be used in iterator methods, if they aren't accessed across `yield return` statement.
15
15
16
16
## Example
17
17
@@ -40,7 +40,7 @@ This enumerator method extracts lines of text from a character array. It naivel
40
40
41
41
## To correct this error
42
42
43
-
`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:
43
+
`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:
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/compiler-messages/cs8176.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,7 @@ helpviewer_keywords:
11
11
12
12
Iterators cannot have by-reference locals
13
13
14
-
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.
14
+
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.
15
15
16
16
Beginning with C# 13, this restriction was removed.
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/compiler-messages/cs8177.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,11 +11,11 @@ helpviewer_keywords:
11
11
12
12
Async methods cannot have by-reference locals
13
13
14
-
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.
14
+
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.
15
15
16
16
## Example
17
17
18
-
The following sample generates CS8177 in C# prior to C# 13:
18
+
The following sample generates CS8177 before C# 13:
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/compiler-messages/warning-waves.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
2
title: "Compiler warning waves"
3
-
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."
3
+
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."
4
4
ms.date: 07/01/2024
5
5
f1_keywords:
6
6
- "CS7023"
@@ -39,7 +39,7 @@ helpviewer_keywords:
39
39
---
40
40
# C# Warning waves
41
41
42
-
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.
42
+
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.
43
43
44
44
## CS9123 - Taking address of local or parameter in async method can create a GC hole.
45
45
@@ -66,7 +66,7 @@ You can address this warning by renaming the type to include at least one non-lo
66
66
67
67
*Warning wave 6*
68
68
69
-
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:
69
+
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:
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:
90
+
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:
91
91
92
92
```csharp
93
93
Console.WriteLine("o is not an instance of a static class");
@@ -97,7 +97,7 @@ Console.WriteLine("o is not an instance of a static class");
97
97
98
98
*Warning wave 5*
99
99
100
-
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 !=`:
100
+
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 !=`:
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/statements/yield.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,11 +22,11 @@ You use the `yield` statement in an [iterator](../../iterators.md) to provide th
22
22
23
23
Iteration also finishes when control reaches the end of an iterator.
24
24
25
-
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:
25
+
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:
<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:
29
+
<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:
30
30
31
31
- You design the type that implements <xref:System.Collections.Generic.IEnumerable%601> or <xref:System.Collections.IEnumerable> interface.
32
32
- 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:
@@ -37,7 +37,7 @@ You can't use the `yield` statements in:
37
37
38
38
- methods with [in](../keywords/method-parameters.md#in-parameter-modifier), [ref](../keywords/ref.md), or [out](../keywords/method-parameters.md#out-parameter-modifier) parameters
39
39
-[lambda expressions](../operators/lambda-expressions.md) and [anonymous methods](../operators/delegate-operator.md)
40
-
-[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.
40
+
-[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.
Copy file name to clipboardExpand all lines: docs/csharp/misc/cs1629.md
+18-21Lines changed: 18 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,31 +6,28 @@ f1_keywords:
6
6
- "CS1629"
7
7
helpviewer_keywords:
8
8
- "CS1629"
9
-
ms.assetid: 907eae46-0265-4cd0-b27b-ff555d004259
10
9
---
11
10
# Compiler Error CS1629
12
11
13
-
Unsafe code may not appear in iterators
12
+
Unsafe code may not appear in iterators
14
13
15
-
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.
16
-
17
-
The C# language specification does not allow unsafe code in iterators.
18
-
19
-
The following sample generates CS1629:
20
-
21
-
```csharp
22
-
// CS1629.cs
14
+
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.
0 commit comments