Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion docs/csharp/misc/cs1023.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Embedded statement cannot be a declaration or labeled statement

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

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.

The following sample generates CS1023 twice:

```csharp
Expand All @@ -24,9 +26,33 @@ public class a
{
if (1)
int i; // CS1023, declaration is not valid here

if (1)
xx : i++; // CS1023, labeled statement is not valid here
}
}
```

## Example - Corrected code

To fix this error, use braces to create a block statement:

```csharp
// CS1023 - Fixed.cs
public class a
{
public static void Main()
{
if (1)
{
int i; // Fixed: declaration is now in a block statement
}

int j = 0;
if (1)
{
xx : j++; // Fixed: labeled statement is now in a block statement
}
}
}
```
Loading