Skip to content

Commit 4dd6663

Browse files
CopilotBillWagner
andauthored
Add resolution guidance to CS1023 compiler error documentation (#47631)
* Initial plan * Add resolution section to CS1023 documentation with corrected examples Co-authored-by: BillWagner <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: BillWagner <[email protected]>
1 parent 570b678 commit 4dd6663

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

docs/csharp/misc/cs1023.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Embedded statement cannot be a declaration or labeled statement
1414

1515
An embedded statement, such as the statements following an **if** statement, can contain neither declarations nor labeled statements.
1616

17+
To resolve this error, wrap the embedded statement in braces to create a block statement. In C#, unlike C/C++, variable declarations and labeled statements must be contained within a block statement to properly define their scope.
18+
1719
The following sample generates CS1023 twice:
1820

1921
```csharp
@@ -24,9 +26,33 @@ public class a
2426
{
2527
if (1)
2628
int i; // CS1023, declaration is not valid here
27-
29+
2830
if (1)
2931
xx : i++; // CS1023, labeled statement is not valid here
3032
}
3133
}
3234
```
35+
36+
## Example - Corrected code
37+
38+
To fix this error, use braces to create a block statement:
39+
40+
```csharp
41+
// CS1023 - Fixed.cs
42+
public class a
43+
{
44+
public static void Main()
45+
{
46+
if (1)
47+
{
48+
int i; // Fixed: declaration is now in a block statement
49+
}
50+
51+
int j = 0;
52+
if (1)
53+
{
54+
xx : j++; // Fixed: labeled statement is now in a block statement
55+
}
56+
}
57+
}
58+
```

0 commit comments

Comments
 (0)