Jon Jagger
jon@jaggersoft.com
Table of Contents 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Notes DownloadECMA-334 C# Language Specificationpreviousnextprevious at this levelnext at this level 15 Statementsprevious at this levelnext at this level 15.1 End points and reachability Paragraph 11 Every statement has an end point. 2 In intuitive terms, the end point of a statement is the location that immediately follows the statement. 3 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. 4 For example, when control reaches the end point of a statement in a block, control is transferred to the next statement in the block. Paragraph 21 If a statement can possibly be reached by execution, the statement is said to be reachable. 2 Conversely, if there is no possibility that a statement will be executed, the statement is said to be unreachable. [Example: In the example
void F() {  
   Console.WriteLine("reachable");  
   goto Label;  
   Console.WriteLine("unreachable");  
   Label:  
   Console.WriteLine("reachable");  
}  
the second invocation of Console.WriteLine is unreachable because there is no possibility that the statement will be executed. end example]
Paragraph 31 A warning is reported if the compiler determines that a statement is unreachable. 2 It is specifically not an error for a statement to be unreachable. [Note: To determine whether a particular statement or end point is reachable, the compiler performs flow analysis according to the reachability rules defined for each statement. The flow analysis takes into account the values of constant expressions (§14.15) that control the behavior of statements, but the possible values of non-constant expressions are not considered. In other words, for purposes of control flow analysis, a non-constant expression of a given type is considered to have any possible value of that type. In the example
void F() {  
   const int i = 1;  
   if (i == 2) Console.WriteLine("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() {  
   int i = 1;  
   if (i == 2) Console.WriteLine("reachable");  
}  
the Console.WriteLine invocation is considered reachable, even though, in reality, it will never be executed. end note]
Paragraph 41 The block of a function member is always considered reachable. 2 By successively evaluating the reachability rules of each statement in a block, the reachability of any given statement can be determined. [Example: In the example
void F(int x) {  
   Console.WriteLine("start");  
   if (x < 0) Console.WriteLine("negative");  
}  
the reachability of the second Console.WriteLine is determined as follows:
Paragraph 51 There are two situations in which it is a compile-time error for the end point of a statement to be reachable:
{ JSL }
Jagger Software Ltd
Company # 4070126
VAT # 762 5213 42
Valid HTML 4.01Valid CSS