diff --git a/docs/csharp/misc/cs1023.md b/docs/csharp/misc/cs1023.md index 71ef35f3ac103..64164c814de3d 100644 --- a/docs/csharp/misc/cs1023.md +++ b/docs/csharp/misc/cs1023.md @@ -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 @@ -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 + } + } +} +```