8.9.4 The return statement
return statement returns control to the caller of the function member in which the
return statement appears.
- return expressionopt ;
return statement with no expression can be used only in a function member that does not compute a value, that is, a method with the return type
set accessor of a property or indexer, the
remove accessors of an event, an instance constructor, a static constructor, or a destructor.
return statement with an expression can only be used in a function member that computes a value, that is, a method with a non-void return type, the
get accessor of a property or indexer, or a user-defined operator. An implicit conversion (Section 6.1) must exist from the type of the expression to the return type of the containing function member.
It is a compile-time error for a
return statement to appear in a
finally block (Section 8.10).
return statement is executed as follows:
- If the
returnstatement specifies an expression, the expression is evaluated and the resulting value is converted to the return type of the containing function member by an implicit conversion. The result of the conversion becomes the value returned to the caller.
- If the
returnstatement is enclosed by one or more
tryblocks with associated
finallyblocks, control is initially transferred to the
finallyblock of the innermost
trystatement. When and if control reaches the end point of a
finallyblock, control is transferred to the
finallyblock of the next enclosing
trystatement. This process is repeated until the
finallyblocks of all enclosing
trystatements have been executed.
- Control is returned to the caller of the containing function member.
return statement unconditionally transfers control elsewhere, the end point of a
return statement is never reachable.