Skip to content

Commit ea1164a

Browse files
committed
Remove restrictions on async methods
Where applicable, remove restrictions on async methods and await statements.
1 parent d27938f commit ea1164a

File tree

7 files changed

+16
-8
lines changed

7 files changed

+16
-8
lines changed

docs/csharp/language-reference/builtin-types/ref-struct.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ You can use the `ref` modifier in the declaration of a [structure type](struct.m
1313
- A `ref struct` can't be boxed to <xref:System.ValueType?displayProperty=nameWithType> or <xref:System.Object?displayProperty=nameWithType>.
1414
- A `ref struct` can't be a type argument.
1515
- 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 an [`async`](../keywords/async.md) 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>.
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>.
1717
- 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.
1818

1919
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`.

docs/csharp/language-reference/compiler-messages/cs1996.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: "Compiler Error CS1996"
33
title: "Compiler Error CS1996"
4-
ms.date: 9/12/2022
4+
ms.date: 7/01/2024
55
f1_keywords:
66
- "CS1996"
77
helpviewer_keywords:
@@ -33,6 +33,8 @@ public class C
3333
}
3434
```
3535

36+
This code still generates this error with C# 13, as the `await` is in the `lock` statement block.
37+
3638
## To correct this error
3739

3840
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:

docs/csharp/language-reference/compiler-messages/cs4004.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public static class C
4747
}
4848
```
4949

50+
This code generates an error in C# 13 because the `await` is in the `unsafe` block.
51+
5052
The `ReverseText` method naively uses a background task to asynchronously create a new string in reverse order of a given string.
5153

5254
## To correct this error

docs/csharp/language-reference/compiler-messages/cs4013.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ helpviewer_keywords:
1111

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

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, provided that they are not in scope during a `yield return` statement.
1515

1616
## Example
1717

docs/csharp/language-reference/compiler-messages/cs8177.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: "Compiler Error CS8177"
33
title: "Compiler Error CS8177"
4-
ms.date: 9/19/2022
4+
ms.date: 7/01/2024
55
f1_keywords:
66
- "CS8177"
77
helpviewer_keywords:
@@ -15,7 +15,7 @@ To manage asynchronous state, `async` methods use a state machine, capturing var
1515

1616
## Example
1717

18-
The following sample generates CS8177:
18+
The following sample generates CS8177 in C# prior to C# 13:
1919

2020
```csharp
2121
// CS8177.cs (20,26)
@@ -49,7 +49,7 @@ class C
4949

5050
## To correct this error
5151

52-
Changing the variable declaration to remove the `ref` modifier corrects this error:
52+
Changing the variable declaration to remove the `ref` modifier corrects this error. Or, you can upgrade to C# 13, which ships with .NET 9.
5353

5454
```csharp
5555
class C

docs/csharp/language-reference/compiler-messages/warning-waves.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Compiler warning waves"
33
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."
4-
ms.date: 05/11/2022
4+
ms.date: 07/01/2024
55
f1_keywords:
66
- "CS7023"
77
- "CS8073"
@@ -50,6 +50,8 @@ The following code produces CS9123:
5050

5151
:::code language="csharp" source="./snippets/WarningWaves/WaveEight.cs" id="NoAmpersand":::
5252

53+
Beginning with C# 13, this code generates a compiler error.
54+
5355
## CS8981 - The type name only contains lower-cased ascii characters.
5456

5557
*Warning wave 7*

docs/csharp/language-reference/keywords/method-parameters.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ When you use these modifiers, they describe how the argument is used:
7979
- `ref readonly` means the method reads, but can't write the value of the argument. The argument *should* be passed by reference.
8080
- `in` means the method reads, but can't write the value of the argument. The argument will be passed by reference or through a temporary variable.
8181

82-
Properties aren't variables. They're methods, and can't be passed to `ref` parameters. You can't use the previous parameter modifiers in the following kinds of methods:
82+
You can't use the previous parameter modifiers in the following kinds of methods:
8383

8484
- Async methods, which you define by using the [async](async.md) modifier.
8585
- Iterator methods, which include a [yield return](../statements/yield.md) or `yield break` statement.
@@ -91,6 +91,8 @@ Properties aren't variables. They're methods, and can't be passed to `ref` param
9191
- The `ref readonly` and `in` keywords can't be used unless the first argument is a `struct`.
9292
- The `ref readonly` and `in` keywords can't be used on any generic type, even when constrained to be a struct.
9393

94+
Properties aren't variables. They're methods. Properties can't be arguments for `ref` parameters.
95+
9496
### `ref` parameter modifier
9597

9698
To use a `ref` parameter, both the method definition and the calling method must explicitly use the `ref` keyword, as shown in the following example. (Except that the calling method can omit `ref` when making a COM call.)

0 commit comments

Comments
 (0)