Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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
breakorcontinuestatement inside awhile,do,for, orforeachloop. Abreakstatement can also appear in aswitchsection. A barebreakorcontinuethat 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 orswitchstatement. The label must name a loop orswitchthat contains thebreakstatement (CS9393). - When you write a labeled
continue(continue label;), apply the same label to an enclosing loop. A labeledcontinuecan target only a loop, not aswitchstatement, becausecontinuestarts 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 caseandgoto defaultonly inside aswitchstatement. Both forms name a section of the enclosingswitchstatement, so they have no meaning outside one (CS0153). - Add a label that matches the name in the
gotostatement, and confirm that the label is within the scope of thegotostatement. Agotostatement can't jump into a nested block, andgoto caserequires a matchingcaselabel in the sameswitchstatement (CS0159). - Give the
goto casevalue the same type as theswitchgoverning expression, or add an explicit cast so an implicit conversion exists. For example, usegoto case (char)127;rather thangoto case 127;when theswitchgoverns acharvalue (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
returnstatement of a member that has a non-voidreturn type, and make sure the value is convertible to that return type. Agetaccessor and a value-returning method both require areturnstatement that includes a value (CS0126). - Remove the value from any
returnstatement in a member that returnsvoid, such as asetaccessor or a method with avoidreturn type. Use a barereturn;statement to exit early (CS0127). - Make sure every code path in a value-returning member ends with a
returnstatement or athrowstatement. A member returns on all code paths when each branch of every conditional statement returns a value, or when a finalreturnstatement 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.