Edit

Share via


Compiler Error CS1023

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:

// CS1023.cs  
public class a  
{  
   public static void Main()  
   {  
      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:

// 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  
      }
   }  
}