disable broken tests on net_4_0
[mcs.git] / docs / ecma334 / 14.5.2.1.xml
blob299d55c8bdd11e27e94f8bf22897bf089ed9cba3
1 <?xml version="1.0"?>
2 <clause number="14.5.2.1" title="Invariant meaning in blocks">
3   <paragraph>For each occurrence of a given identifier as a <non_terminal where="14.5.2">simple-name</non_terminal> in an expression, every other occurrence of the same identifier as a <non_terminal where="14.5.2">simple-name</non_terminal> in an expression within the immediately enclosing block (<hyperlink>15.2</hyperlink>) or  <non_terminal where="15.7.2">switch-block</non_terminal> (<hyperlink>15.7.2</hyperlink>) must refer to the same entity. This rule ensures that the meaning of a name in the context of an expression is always the same within a block. </paragraph>
4   <paragraph>The example <code_example><![CDATA[
5 class Test  
6 {  
7    double x;  
8    void F(bool b) {  
9       x = 1.0;  
10       if (b) {  
11          int x = 1;  
12       }  
13    }  
14 }  
15 ]]></code_example>results in a compile-time error because x refers to different entities within the outer block (the extent of which includes the nested block in the if statement). In contrast, the example <code_example><![CDATA[
16 class Test  
17 {  
18    double x;  
19    void F(bool b) {  
20       if (b) {  
21          x = 1.0;  
22       }  
23       else {  
24          int x = 1;  
25       }  
26    }  
27 }  
28 ]]></code_example>is permitted because the name x is never used in the outer block. </paragraph>
29   <paragraph>Note that the rule of invariant meaning applies only to simple names. It is perfectly valid for the same identifier to have one meaning as a simple name and another meaning as right operand of a member access (<hyperlink>14.5.4</hyperlink>). <example>[Example: For example: <code_example><![CDATA[
30 struct Point  
31 {  
32    int x, y;  
33    public Point(int x, int y) {  
34       this.x = x;  
35       this.y = y;  
36    }  
37 }  
38 ]]></code_example></example></paragraph>
39   <paragraph>
40     <example>The example above illustrates a common pattern of using the names of fields as parameter names in an instance constructor. In the example, the simple names x and y refer to the parameters, but that does not prevent the member access expressions this.x and this.y from accessing the fields. end example]</example>
41   </paragraph>
42 </clause>