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:
- 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
- CS0140: The label 'label' is a duplicate
- CS0153: A goto case is only valid inside a switch statement
- CS0157: Control cannot leave the body of a finally clause
- CS0158: The label 'label' shadows another label by the same name in a contained scope
- CS0159: No such label 'label' within the scope of the goto statement
- CS0161: 'method': not all code paths return a value
- CS0162: Unreachable code detected
- CS0163: Control cannot fall through from one case label ('label') to another
- CS0164: This label has not been referenced
- CS0469: The 'goto case' value is not implicitly convertible to type 'type'
- CS8070: Control cannot fall out of switch from final case label ('label')
- 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
Choose valid jump targets
CS0139: No enclosing loop out of which to break or continue
CS0153: A goto case is only valid inside a switch statement
CS0157: Control cannot leave the body of a finally clause
CS0159: No such label 'label' within the scope of the goto statement
CS0469: The 'goto case' value is not implicitly convertible to type 'type'
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
Place
breakinside a loop orswitchstatement, and placecontinueinside a loop. Each statement needs an enclosing construct to target (CS0139).Apply the label in
break label;to an enclosing loop orswitchstatement (CS9393). Apply the label incontinue label;to an enclosing loop, not aswitchstatement (CS9394).Place
goto caseandgoto defaultinside theswitchstatement that contains their destination (CS0153).Define the named destination within the
gotostatement's scope, or correct the target name. Agotostatement can't enter a nested block or another member (CS0159).Give a
goto casevalue a type that's implicitly convertible to theswitchgoverning type. For example, usegoto case (char)127;rather thangoto case 127;when the destination is a character case label (CS0469).Move a
return,break,continue, orgotothat exits afinallyblock outside that block. Jumps within the block are valid only when their targets also remain inside it (CS0157).
See Jump statements and The try-finally statement for the applicable control-flow rules.
Define and reference labels consistently
CS0140: The label 'label' is a duplicate
CS0158: The label 'label' shadows another label by the same name in a contained scope
CS0164: This label has not been referenced
Rename labels that have the same name in the same or nested scopes, and update the corresponding
gototargets. Remove a duplicate label if the existing label already marks the intended destination (CS0140, CS0158).Remove an unused label while keeping its statement. If a
gotowas intended to reach that statement, correct its target label instead (CS0164).
For label scope and target resolution, see The goto statement.
Terminate switch sections explicitly
- CS0163: Control cannot fall through from one case label ('label') to another
- CS8070: Control cannot fall out of switch from final case label ('label')
Add a jump statement such as break, return, goto case, goto default, or throw wherever control could otherwise reach the end of a switch section. Control can't fall into the next section (CS0163) or out of the final section (CS8070), even when the switch has only one section.
The following examples generate CS8070 at the marked final case 1: and default: labels. Uncomment the final break; in each switch to fix the error:
switch (number)
{
case 0:
number++;
break;
case 1: // CS8070
number = 0;
// break;
}
switch (number)
{
case 0:
number++;
break;
default: // CS8070
number = 0;
// break;
}
To share statements between values, stack their case labels with no intervening statements. To transfer control to another section explicitly, use goto case or goto default.
See The switch statement and The throw statement.
Return the required 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
Supply an expression after
returnwhen the containing member requires a value, and make its type convertible to the required return type (CS0126).Use
return;without an expression in a member that doesn't return a value, such as avoidmethod or a property setter (CS0127).Cover every reachable exit from a value-returning member. Add a return for an uncovered branch, or throw when a branch can't produce a result (CS0161).
For return forms and requirements, see The return statement.
Remove unreachable code
- CS0162: Unreachable code detected
Remove statements that follow an unconditional jump when no control-flow path can reach them. For example, remove break; immediately after return in a switch section; the return already exits the member (CS0162).
If the statements were intended to execute, move them before the jump or correct the condition or jump target that makes them unreachable. See Jump statements for how each statement transfers control.
C# language specification
For more information, see the Jump statements section of the C# language specification.