How to Resolve Error "CS1023: Embedded statement cannot be a declaration or labeled statement" in C#
The Compiler Error CS1023 is a syntax restriction error. The message reads: "Embedded statement cannot be a declaration or labeled statement".
In C#, control flow statements like if, else, while, for, and foreach allow you to execute a single statement without using curly braces {}. This single statement is called an Embedded Statement.
However, C# creates a strict rule: You cannot declare a variable or define a label inside an embedded statement. This is because a variable declared in a single-line statement would immediately go out of scope on the very next line, rendering it useless. To declare variables inside a conditional or loop, you must create a block scope using curly braces.
This guide explains the rule and how to fix your control flow blocks.
Understanding Embedded Statements
- Block Statement: Code wrapped in
{ ... }. This creates a new scope. - Embedded Statement: A single line following a control keyword (e.g.,
if (true) DoSomething();).
If you write if (true) int x = 5;, the variable x technically exists only for that one line. By the time the semicolon is reached, x is destroyed. The compiler treats this as a likely logic error and forbids the syntax entirely.
Scenario 1: Variable Declaration in if or else
This is the most common cause. You want to calculate a value only if a condition is met, so you try to declare the variable directly after the if statement without braces.
Example of error
public void Process(bool isValid)
{
// ⛔️ Error CS1023: Embedded statement cannot be a declaration.
// The compiler forbids declaring 'result' here because it lacks { }.
if (isValid)
int result = 100;
// Also happens with 'else'
else
string message = "Error";
}
Solution: Add Curly Braces
To declare a variable inside a conditional branch, you must wrap the branch in a block { ... }.
public void Process(bool isValid)
{
// ✅ Correct: The braces create a valid scope for 'result'.
if (isValid)
{
int result = 100;
Console.WriteLine(result);
}
else
{
string message = "Error";
Console.WriteLine(message);
}
}
Scope Warning: Variables declared inside { ... } are only visible inside those braces. If you need to use the variable after the if statement, you must declare it before the if statement.
Scenario 2: Declaration inside Loops
Similarly, attempting to declare a temporary variable inside a single-line loop is invalid.
Example of error
public void LoopData(List<string> items)
{
// ⛔️ Error CS1023: Cannot declare 'temp' without braces.
foreach (var item in items)
var temp = item.Trim();
}
Solution: Add Curly Braces
Wrap the loop body.
public void LoopData(List<string> items)
{
// ✅ Correct: Loop body is now a block.
foreach (var item in items)
{
var temp = item.Trim();
Console.WriteLine(temp);
}
}
Scenario 3: Labeled Statements
A Labeled Statement (used with goto) is technically a marker, not an executable action. Therefore, it cannot stand alone as the single embedded statement of a control structure.
Example of error
public void CheckStatus(int status)
{
// ⛔️ Error CS1023: A label cannot be the immediate child of an 'if'.
if (status == 0)
MyLabel: Console.WriteLine("Zero");
}
Solution: Add Curly Braces
Even labels must be enclosed in a block if they are inside a control structure.
public void CheckStatus(int status)
{
// ✅ Correct
if (status == 0)
{
MyLabel:
Console.WriteLine("Zero");
}
}
Conclusion
CS1023 is the compiler enforcing useful scoping rules.
- The Rule: You cannot declare variables immediately after
if,else,for,foreach, orwhilewithout braces. - The Fix: Always add Curly Braces
{ ... }around the body of your control statements if you need to declare variables or define labels inside them.