ECMA-334 C# Language Specification15.1: End points and reachability |
Every statement has an end point. In intuitive terms, the end point of a statement is the location that immediately follows the statement. The execution rules for composite statements (statements that contain embedded statements) specify the action that is taken when control reaches the end point of an embedded statement. For example, when control reaches the end point of a statement in a block, control is transferred to the next statement in the block.
If a statement can possibly be reached by execution, the statement is said to be reachable. Conversely, if there is no possibility that a statement will be executed, the statement is said to be unreachable.
the second invocation of Console.WriteLine is unreachable because there is no possibility that the statement will be executed. end example]
void F() {
Console.WriteLine("reachable");
goto Label;
Console.WriteLine("unreachable");
Label:
Console.WriteLine("reachable");
}
A warning is reported if the compiler determines that a statement is unreachable. It is specifically not an error for a statement to be unreachable.
the boolean expression of the if statement is a constant expression because both operands of the == operator are constants. As the constant expression is evaluated at compile-time, producing the value false, the Console.WriteLine invocation is considered unreachable. However, if i is changed to be a local variable
void F() {
const int i = 1;
if (i == 2) Console.WriteLine("unreachable");
}
the Console.WriteLine invocation is considered reachable, even though, in reality, it will never be executed. end note]
void F() {
int i = 1;
if (i == 2) Console.WriteLine("reachable");
}
The block of a function member is always considered reachable. By successively evaluating the reachability rules of each statement in a block, the reachability of any given statement can be determined.
the reachability of the second Console.WriteLine is determined as follows:
void F(int x) {
Console.WriteLine("start");
if (x < 0) Console.WriteLine("negative");
}
There are two situations in which it is a compile-time error for the end point of a statement to be reachable: