disable broken tests on net_4_0
[mcs.git] / docs / ecma334 / 17.5.1.2.xml
blob178ae21a139263137b0df914a1a23f6837ab0e0a
1 <?xml version="1.0"?>
2 <clause number="17.5.1.2" title="Reference parameters">
3   <paragraph>A parameter declared with a ref modifier is a reference parameter. Unlike a value parameter, a reference parameter does not create a new storage location. Instead, a reference parameter represents the same storage location as the variable given as the argument in the method invocation. </paragraph>
4   <paragraph>When a formal parameter is a reference parameter, the corresponding argument in a method invocation must consist of the keyword ref followed by a <non_terminal where="12.4">variable-reference</non_terminal> (<hyperlink>12.3.3</hyperlink>) of the same type as the formal parameter. A variable must be definitely assigned before it can be passed as a reference parameter. </paragraph>
5   <paragraph>Within a method, a reference parameter is always considered definitely assigned. </paragraph>
6   <paragraph>
7     <example>[Example: The example <code_example><![CDATA[
8 using System;  
9 class Test  
10 {  
11    static void Swap(ref int x, ref int y) {  
12       int temp = x;  
13       x = y;  
14       y = temp;  
15    }  
16    static void Main() {  
17       int i = 1, j = 2;  
18       Swap(ref i, ref j);  
19       Console.WriteLine("i = {0}, j = {1}", i, j);  
20    }  
21 }  
22 ]]></code_example>produces the output <code_example><![CDATA[
23 i = 2, j = 1  
24 ]]></code_example></example>
25   </paragraph>
26   <paragraph>
27     <example>For the invocation of Swap in Main, x represents i and y represents j. Thus, the invocation has the effect of swapping the values of i and j. end example]</example>
28   </paragraph>
29   <paragraph>In a method that takes reference parameters, it is possible for multiple names to represent the same storage location. <example>[Example: In the example <code_example><![CDATA[
30 class A  
31 {  
32    string s;  
33    void F(ref string a, ref string b) {  
34       s = "One";  
35       a = "Two";  
36       b = "Three";  
37    }  
38    void G() {  
39       F(ref s, ref s);  
40    }  
41 }  
42 ]]></code_example>the invocation of F in G passes a reference to s for both a and b. Thus, for that invocation, the names s, a, and b all refer to the same storage location, and the three assignments all modify the instance field s. end example]</example> </paragraph>
43 </clause>