13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>C#</strong> LANGUAGE SPECIFICATIONthe second invocation of Console.WriteLine is unreachable because there is no possibility that thestatement will be executed. end example]A warning is reported if the compiler determines that a statement is unreachable. It is specifically not anerror for a statement to be unreachable.[Note: To determine whether a particular statement or end point is reachable, the compiler performs flowanalysis according to the reachability rules defined for each statement. The flow analysis takes into accountthe values of constant expressions (§14.15) that control the behavior of statements, but the possible values ofnon-constant expressions are not considered. In other words, for purposes of control flow analysis, a nonconstantexpression of a given type is considered to have any possible value of that type.In the examplevoid 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 valuefalse, the Console.WriteLine invocation is considered unreachable. However, if i is changed to be alocal variablevoid F() {int i = 1;if (i == 2) Console.WriteLine("reachable");}the Console.WriteLine invocation is considered reachable, even though, in reality, it will never beexecuted. end note]The block of a function member is always considered reachable. By successively evaluating the reachabilityrules of each statement in a block, the reachability of any given statement can be determined.[Example: In the examplevoid F(int x) {Console.WriteLine("start");if (x < 0) Console.WriteLine("negative");}the reachability of the second Console.WriteLine is determined as follows:• The first Console.WriteLine expression statement is reachable because the block of the F method isreachable (§15.2).• The end point of the first Console.WriteLine expression statement is reachable15.2 because thatstatement is reachable (§15.6 and §15.2).• The if statement is reachable because the end point of the first Console.WriteLine expressionstatement is reachable (§15.6 and §15.2).• The second Console.WriteLine expression statement is reachable because the boolean expression ofthe if statement does not have the constant value false.end example]There are two situations in which it is a compile-time error for the end point of a statement to be reachable:• Because the switch statement does not permit a switch section to “fall through” to the next switchsection, it is a compile-time error for the end point of the statement list of a switch section to bereachable. If this error occurs, it is typically an indication that a break statement is missing.• It is a compile-time error for the end point of the block of a function member that computes a value to bereachable. If this error occurs, it typically is an indication that a return statement is missing.178

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!