Skip to main content

How to Resolve Warning "CS0642: Possible mistaken empty statement" in C#

The Compiler Warning CS0642 is a logic safety warning. The message reads: "Possible mistaken empty statement".

In C#, the semicolon ; marks the end of a statement. If you place a semicolon immediately after a control flow statement like if, while, for, or foreach, the compiler interprets that as: "Check the condition, and then do nothing."

This warning exists because, in 99% of cases, this is a typo. The developer likely intended the code on the next line to be controlled by the if statement, but the accidental semicolon severed the connection, causing the subsequent code to execute unconditionally.

Understanding the Logic Error

C# ignores indentation. The following two snippets behave identically to the compiler, but very differently to the human reader:

What you see:

if (isReady)
LaunchRocket(); // Looks like it depends on isReady

What the compiler sees if you add a semicolon:

if (isReady);       // Statement 1: If ready, do nothing.
LaunchRocket(); // Statement 2: Execute this line always.

Scenario 1: The Accidental 'if' Termination

This is the most frequent cause. A developer creates a conditional check but habitually ends the line with a semicolon.

Example of warning:

public void ProcessPayment(bool isSuccess)
{
// ⛔️ Warning CS0642: Possible mistaken empty statement.
// The semicolon terminates the 'if' logic immediately.
if (!isSuccess);

// This block is NOT part of the 'if'. It executes every time.
{
System.Console.WriteLine("Payment Failed. Rollback.");
}
}

Result: Even if isSuccess is true, the console prints "Payment Failed".

Scenario 2: The Infinite 'while' Loop

This error is even more dangerous with loops. If you terminate a while loop with a semicolon, the loop body becomes empty. If the condition is true initially, the program enters an infinite loop doing nothing, hanging the application.

Example of warning:

public void WaitForSignal()
{
bool signal = true;

// ⛔️ Warning CS0642
// If 'signal' is true, this loop spins forever.
// It never reaches the block { } to change the signal or break.
while (signal);
{
// This code is unreachable
signal = CheckHardware();
}
}

Solution 1: Remove the Semicolon

In almost all cases, the fix is simply to delete the stray semicolon so that the following block becomes the body of the statement.

Solution:

public void ProcessPayment(bool isSuccess)
{
// ✅ Correct: The block is now controlled by the condition.
if (!isSuccess)
{
System.Console.WriteLine("Payment Failed. Rollback.");
}
}

Solution 2: Intentional No-Op (Explicit Syntax)

Very rarely, you might actually want a loop that does nothing (e.g., a "busy wait" spinlock, though SpinWait is preferred) or an if statement where you want to do nothing for a specific case but handle else.

To suppress the warning, make your "empty statement" explicit by using an empty block { }.

Solution:

public void BusyWait()
{
// ✅ Correct: Using braces tells the compiler
// "I intentionally want the body of this loop to be empty."
while (!IsDataReady())
{
}
}
note

Code Style: Some developers use a semicolon on a new line to indicate intent:

if (condition)
; // Intentional empty statement

However, using empty braces { } is generally considered clearer and safer.

Conclusion

CS0642 is your compiler saving you from a subtle but devastating logic bug.

  1. Check your lines: Did you put a ; after if (...)?
  2. Delete it: If you want the next line to run conditionally, execute the semicolon.
  3. Use Braces: Always wrapping your if and while blocks in { ... } prevents this warning entirely, because if (...) {;} is valid (empty block) but if (...); { is visually obvious as incorrect.