Edit

Resolve errors and warnings for jump statements

This article covers the following compiler errors and warnings:

The following errors occur when you use a break, continue, goto, or return statement in a way that doesn't match the surrounding control flow:

  • CS0126: An object of a type convertible to 'type' is required
  • CS0127: Since 'method' returns void, a return keyword must not be followed by an object expression
  • CS0139: No enclosing loop out of which to break or continue
  • CS0153: A goto case is only valid inside a switch statement
  • CS0159: No such label 'label' within the scope of the goto statement
  • CS0161: 'method': not all code paths return a value
  • CS0163: Control cannot fall through from one case label ('label') to another
  • CS9393: No enclosing loop or switch statement with the label 'label' out of which to break
  • CS9394: No enclosing loop with the label 'label' out of which to continue

The following warning occurs when a goto case statement targets a value that requires a conversion:

  • CS0469: The 'goto case' value is not implicitly convertible to type 'type'

Break and continue targets

  • CS0139: No enclosing loop out of which to break or continue
  • CS9393: No enclosing loop or switch statement with the label 'label' out of which to break
  • CS9394: No enclosing loop with the label 'label' out of which to continue

The break statement transfers control out of the nearest enclosing loop or switch statement, and the continue statement starts the next iteration of the nearest enclosing loop. The compiler reports these diagnostics when it can't find that target.

To resolve these errors:

  • Place the break or continue statement inside a while, do, for, or foreach loop. A break statement can also appear in a switch section. A bare break or continue that isn't nested in one of these statements has no target (CS0139).
  • When you write a labeled break (break label;), apply the same label to an enclosing loop or switch statement. The label must name a loop or switch that contains the break statement (CS9393).
  • When you write a labeled continue (continue label;), apply the same label to an enclosing loop. A labeled continue can target only a loop, not a switch statement, because continue starts the next loop iteration (CS9394).

For more information about these statements, see The break statement and The continue statement in the jump statements article.

Goto statement targets

  • CS0153: A goto case is only valid inside a switch statement
  • CS0159: No such label 'label' within the scope of the goto statement
  • CS0469: The 'goto case' value is not implicitly convertible to type 'type'

The goto statement transfers control to a labeled statement, to a case label, or to the default label of a switch statement. The compiler reports these diagnostics when the target label doesn't exist or isn't reachable from the goto statement.

To resolve these errors:

  • Use goto case and goto default only inside a switch statement. Both forms name a section of the enclosing switch statement, so they have no meaning outside one (CS0153).
  • Add a label that matches the name in the goto statement, and confirm that the label is within the scope of the goto statement. A goto statement can't jump into a nested block, and goto case requires a matching case label in the same switch statement (CS0159).
  • Give the goto case value the same type as the switch governing expression, or add an explicit cast so an implicit conversion exists. For example, use goto case (char)127; rather than goto case 127; when the switch governs a char value (CS0469).

For more information about the goto statement, see The goto statement in the jump statements article. For more information about goto case and goto default, see The switch statement in the selection statements article.

Switch section termination

  • CS0163: Control cannot fall through from one case label ('label') to another

C# doesn't allow control to fall through from one switch section to the next. When a switch statement contains more than one section, you must explicitly terminate each section, including the last one.

To resolve this error, end each switch section that contains statements with a jump statement, such as break, return, goto case, goto default, or throw. To run the same statements for more than one value, stack the case labels with no statements between them; that arrangement isn't fall-through. When you want one section to continue into another, use goto case or goto default to name the next section explicitly (CS0163).

For more information about the switch statement, see The switch statement in the selection statements article. For more information about the throw statement, see The throw statement in the exception handling statements article.

Return statement values

  • CS0126: An object of a type convertible to 'type' is required
  • CS0127: Since 'method' returns void, a return keyword must not be followed by an object expression
  • CS0161: 'method': not all code paths return a value

The return statement ends execution of the containing member and returns control to the caller. A return statement can also return a value.

To resolve these errors:

  • Supply a value in each return statement of a member that has a non-void return type, and make sure the value is convertible to that return type. A get accessor and a value-returning method both require a return statement that includes a value (CS0126).
  • Remove the value from any return statement in a member that returns void, such as a set accessor or a method with a void return type. Use a bare return; statement to exit early (CS0127).
  • Make sure every code path in a value-returning member ends with a return statement or a throw statement. A member returns on all code paths when each branch of every conditional statement returns a value, or when a final return statement follows the conditional logic (CS0161).

For more information about return statements in async methods and members that return Task or ValueTask types, see Errors and warnings related to async and await. For more information about return statements in iterators, see Errors and warnings related to iterators.

For more information about the return statement, see The return statement in the jump statements article.

C# language specification

For more information, see the Jump statements section of the C# language specification.

See also