Skip to main content

How to Resolve Error "CS0159: No such label 'label' within the scope of the goto statement" in C#

The Compiler Error CS0159 is a flow control scope error. The message reads: "No such label 'LabelName' within the scope of the goto statement".

In C#, the goto statement allows you to transfer control to a specific line of code marked by a Label (identifier followed by a colon). However, you cannot jump to just any label in your code. The label must be reachable according to scoping rules.

This error occurs in two main situations:

  1. Typos: The label simply doesn't exist (spelling mistake).
  2. Scope Restriction: The label exists, but it is buried inside a nested block (like an if, for, or try block) that you are not allowed to jump into from the outside.

Understanding Label Reachability

C# scoping rules dictate where you can jump:

  • Allowed: Jumping within the same block.
  • Allowed: Jumping OUT of a nested block to an outer block.
  • Forbidden: Jumping INTO a nested block from an outer block.

The compiler prevents jumping into blocks because it would bypass initialization logic, conditional checks, or iteration setups defined at the start of that block.

Scenario 1: Typos and Missing Labels

The simplest cause is a spelling mismatch between the goto command and the label definition. Remember that C# identifiers are case-sensitive.

Example of error

public void RunProcess()
{
// ⛔️ Error CS0159: No such label 'Finish' within the scope...
// The label below is spelled 'Finished' (past tense).
goto Finish;

Console.WriteLine("Working...");

Finished:
Console.WriteLine("Done.");
}

Solution: Correct the Name

Ensure the spelling matches exactly.

public void RunProcess()
{
// ✅ Correct: Matches the label definition
goto Finished;

Console.WriteLine("Working...");

Finished:
Console.WriteLine("Done.");
}

Scenario 2: Jumping INTO a Nested Block

This is the more complex structural error. You defined the label inside an if statement, loop, or try block, but you are trying to jump to it from outside that block.

Example of error

Trying to jump into an if block.

public void Validate(bool isValid)
{
// ⛔️ Error CS0159: The label 'Target' is inside the 'if' block.
// You cannot jump INTO the 'if' block from here because
// it would bypass the 'isValid' check.
goto Target;

if (isValid)
{
Target:
Console.WriteLine("Valid or Jumped To");
}
}

Solution: Move the Label

To fix this, you must move the label to a scope that is visible to the goto statement (usually the outer scope).

public void Validate(bool isValid)
{
if (isValid)
{
// Logic...
}

// ✅ Correct: We jump over the logic block to a label in the same scope.
goto Target;

Target:
Console.WriteLine("Valid or Jumped To");
}
note

Jumping Out is Allowed: It is perfectly valid to have goto Target; inside the if block and Target: outside it. This is how break effectively works under the hood.

Special Case: Switch Statements

You cannot jump directly into a specific case of a switch statement using goto Label;. You must use goto case Value;.

// Invalid
goto MyLabel;
switch(x) { case 1: MyLabel: break; }
// Valid
/witch(x) { case 1: goto case 2; case 2: ... }

Conclusion

CS0159 is the compiler enforcing logical flow safety.

  1. Check Spelling: Verify goto Name; matches Name: exactly (including case).
  2. Check Scope: Look at where the label is defined. Is it inside curly braces { }?
  3. Fix Direction: Ensure you aren't trying to jump into those braces from the outside. Move the label out to the parent scope if necessary.