Skip to content

Commit 7792145

Browse files
committed
decided to add this one as well.
1 parent cc9a675 commit 7792145

File tree

4 files changed

+88
-93
lines changed

4 files changed

+88
-93
lines changed

.openpublishing.redirection.csharp.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,10 @@
491491
"source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1988.md",
492492
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/ref-modifiers-errors"
493493
},
494+
{
495+
"source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs4004.md",
496+
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/unsafe-code-errors"
497+
},
494498
{
495499
"source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs3007.md",
496500
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/array-declaration-errors"

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

Lines changed: 0 additions & 88 deletions
This file was deleted.

docs/csharp/language-reference/compiler-messages/unsafe-code-errors.md

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ f1_keywords:
2525
- "CS1665"
2626
- "CS1666"
2727
- "CS1708"
28+
- "CS4004"
2829
- "CS1716"
2930
- "CS1919"
3031
- "CS8812"
31-
- "CS9123" # The '&' operator should not be used on parameters or local variables in async methods. (new unsafe file)
32+
- "CS9123"
3233
helpviewer_keywords:
3334
- "CS0193"
3435
- "CS0196"
@@ -54,9 +55,10 @@ helpviewer_keywords:
5455
- "CS1666"
5556
- "CS1708"
5657
- "CS1716"
58+
- "CS4004"
5759
- "CS1919"
5860
- "CS8812"
59-
- "CS9123" # The '&' operator should not be used on parameters or local variables in async methods. (new unsafe file)
61+
- "CS9123"
6062
ms.date: 10/21/2025
6163
---
6264
# Resolve errors and warnings in unsafe code constructs
@@ -91,6 +93,7 @@ That's by design. The text closely matches the text of the compiler error / warn
9193
- [**CS1708**](#cs1708): *Fixed size buffers can only be accessed through locals or fields*
9294
- [**CS1716**](#cs1716): *Do not use 'System.Runtime.CompilerServices.FixedBuffer' attribute. Use the 'fixed' field modifier instead.*
9395
- [**CS1919**](#cs1919): *Unsafe type 'type name' cannot be used in object creation.*
96+
- [**CS4004**](#cs4004): *Cannot await in an unsafe context*
9497
- [**CS8812**](#cs8812): *Cannot convert `&Method` group to non-function pointer type.*
9598
- **CS9123**: *The '`&`' operator should not be used on parameters or local variables in async methods.
9699

@@ -1062,6 +1065,84 @@ unsafe public class C
10621065

10631066
- [Interoperability](../../advanced-topics/interop/index.md)
10641067

1068+
## CS4004
1069+
1070+
Cannot await in an unsafe context
1071+
1072+
This error occurs when you use the `await` keyword in an [`unsafe` context](../keywords/unsafe.md). The compiler can't allow await operations in unsafe contexts.
1073+
1074+
### Example
1075+
1076+
The following sample generates CS4004:
1077+
1078+
```csharp
1079+
using System.Threading.Tasks;
1080+
1081+
public static class C
1082+
{
1083+
public static unsafe async Task<string> ReverseTextAsync(string text)
1084+
{
1085+
return await Task.Run(() =>
1086+
{
1087+
if (string.IsNullOrEmpty(text))
1088+
{
1089+
return text;
1090+
}
1091+
1092+
fixed (char* pText = text)
1093+
{
1094+
char* pStart = pText;
1095+
char* pEnd = pText + text.Length - 1;
1096+
for (int i = text.Length / 2; i >= 0; i--)
1097+
{
1098+
char temp = *pStart;
1099+
*pStart++ = *pEnd;
1100+
*pEnd-- = temp;
1101+
}
1102+
1103+
return text;
1104+
}
1105+
});
1106+
}
1107+
}
1108+
```
1109+
1110+
### To correct this error
1111+
1112+
Separate the unsafe code from the awaitable code. One technique is creating a new method for the unsafe code and then calling it from the awaitable code. For example:
1113+
1114+
```csharp
1115+
public static class C
1116+
{
1117+
public static async Task<string> ReverseTextAsync(string text)
1118+
{
1119+
return await Task.Run(() => ReverseTextUnsafe(text));
1120+
}
1121+
1122+
private static unsafe string ReverseTextUnsafe(string text)
1123+
{
1124+
if (string.IsNullOrEmpty(text))
1125+
{
1126+
return text;
1127+
}
1128+
1129+
fixed (char* pText = text)
1130+
{
1131+
char* pStart = pText;
1132+
char* pEnd = pText + text.Length - 1;
1133+
for (int i = text.Length / 2; i >= 0; i--)
1134+
{
1135+
char temp = *pStart;
1136+
*pStart++ = *pEnd;
1137+
*pEnd-- = temp;
1138+
}
1139+
1140+
return text;
1141+
}
1142+
}
1143+
}
1144+
```
1145+
10651146
## CS8812
10661147

10671148
Cannot convert `&Method` group to non-function pointer type.

docs/csharp/language-reference/toc.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ items:
595595
displayName: CS0185, CS9216, CS9217
596596
- name: Unsafe code
597597
href: ./compiler-messages/unsafe-code-errors.md
598-
displayName: CS0193, CS0196, CS0208, CS0209, CS0210, CS0211, CS0212, CS0213, CS0214, CS0227, CS0233, CS0242, CS0244, CS0254, CS0459, CS0821, CS1641, CS1642, CS1656, CS1663, CS1665, CS1666, CS1708, CS1716, CS1919, CS8812, CS9123
598+
displayName: CS0193, CS0196, CS0208, CS0209, CS0210, CS0211, CS0212, CS0213, CS0214, CS0227, CS0233, CS0242, CS0244, CS0254, CS0459, CS0821, CS1641, CS1642, CS1656, CS1663, CS1665, CS1666, CS1708, CS1716, CS1919, CS4004, CS8812, CS9123
599599
- name: Warning waves
600600
href: ./compiler-messages/warning-waves.md
601601
displayName: >
@@ -1777,8 +1777,6 @@ items:
17771777
href: ../misc/cs2035.md
17781778
- name: CS2036
17791779
href: ../misc/cs2036.md
1780-
- name: CS4004
1781-
href: ./compiler-messages/cs4004.md
17821780
- name: CS4008
17831781
href: ./compiler-messages/cs4008.md
17841782
- name: CS4009

0 commit comments

Comments
 (0)