disable broken tests on net_4_0
[mcs.git] / docs / ecma334 / 8.3.xml
blob200ce6868e8279f9c0548e5b93b39f1f05bf0123
1 <?xml version="1.0"?>
2 <clause number="8.3" title="Variables and parameters" informative="true">
3   <paragraph>Variables represent storage locations. Every variable has a type that determines what values can be stored in the variable. Local variables are variables that are declared in methods, properties, or indexers. A local variable is defined by specifying a type name and a declarator that specifies the variable name and an optional initial value, as in: <code_example><![CDATA[
4 int a;  
5 int b = 1;  
6 ]]></code_example>but it is also possible for a local variable declaration to include multiple declarators. The declarations of a and b can be rewritten as: <code_example><![CDATA[
7 int a, b = 1;  
8 ]]></code_example></paragraph>
9   <paragraph>A variable must be assigned before its value can be obtained. The example <code_example><![CDATA[
10 class Test  
11 {  
12    static void Main() {  
13       int a;  
14       int b = 1;  
15       int c = a + b; // error, a not yet assigned  
16       ...  
17    }  
18 }  
19 ]]></code_example>results in a compile-time error because it attempts to use the variable a before it is assigned a value. The rules governing definite assignment are defined in <hyperlink>12.3</hyperlink>. </paragraph>
20   <paragraph>A field (<hyperlink>17.4</hyperlink>) is a variable that is associated with a class or struct, or an instance of a class or struct. A field declared with the static modifier defines a static variable, and a field declared without this modifier defines an instance variable. A static field is associated with a type, whereas an instance variable is associated with an instance. The example <code_example><![CDATA[
21 using Personnel.Data;  
22 class Employee  
23 {  
24    private static DataSet ds;  
25    public string Name;  
26    public decimal Salary;  
27    ...  
28 }  
29 ]]></code_example>shows an Employee class that has a private static variable and two public instance variables. </paragraph>
30   <paragraph>Formal parameter declarations also define variables. There are four kinds of parameters: value parameters, reference parameters, output parameters, and parameter arrays. </paragraph>
31   <paragraph>A value parameter is used for &quot;in&quot; parameter passing, in which the value of an argument is passed into a method, and modifications of the parameter do not impact the original argument. A value parameter refers to its own variable, one that is distinct from the corresponding argument. This variable is initialized by copying the value of the corresponding argument. The example <code_example><![CDATA[
32 using System;  
33 class Test {  
34    static void F(int p) {  
35       Console.WriteLine("p = {0}", p);  
36       p++;  
37    }  
38    static void Main() {  
39       int a = 1;  
40       Console.WriteLine("pre:  a = {0}", a);  
41       F(a);  
42       Console.WriteLine("post: a = {0}", a);  
43    }  
44 }  
45 ]]></code_example>shows a method F that has a value parameter named p. The example produces the output: <code_example><![CDATA[
46 pre:  a = 1  
47 p = 1  
48 post: a = 1  
49 ]]></code_example>even though the value parameter p is modified. </paragraph>
50   <paragraph>A reference parameter is used for &quot;by reference&quot; parameter passing, in which the parameter acts as an alias for a caller-provided argument. A reference parameter does not itself define a variable, but rather refers to the variable of the corresponding argument. Modifications of a reference parameter impact the corresponding argument. A reference parameter is declared with a ref modifier. The example <code_example><![CDATA[
51 using System;  
52 class Test {  
53    static void Swap(ref int a, ref int b) {  
54       int t = a;  
55       a = b;  
56       b = t;  
57    }  
58    static void Main() {  
59       int x = 1;  
60       int y = 2;  
61       
62       Console.WriteLine("pre:  x = {0}, y = {1}", x, y);  
63       Swap(ref x, ref y);  
64       Console.WriteLine("post: x = {0}, y = {1}", x, y);  
65    }  
66 }  
67 ]]></code_example>shows a Swap method that has two reference parameters. The output produced is: <code_example><![CDATA[
68 pre:  x = 1, y = 2  
69 post: x = 2, y = 1  
70 ]]></code_example></paragraph>
71   <paragraph>The ref keyword must be used in both the declaration of the formal parameter and in uses of it. The use of ref at the call site calls special attention to the parameter, so that a developer reading the code will understand that the value of the argument could change as a result of the call. </paragraph>
72   <paragraph>An output parameter is similar to a reference parameter, except that the initial value of the caller-provided argument is unimportant. An output parameter is declared with an out modifier. The example <code_example><![CDATA[
73 using System;  
74 class Test {  
75    static void Divide(int a, int b, out int result, out int remainder) {  
76       result = a / b;  
77       remainder = a % b;  
78    }  
79    static void Main() {  
80       for (int i = 1; i < 10; i++)  
81       for (int j = 1; j < 10; j++) {  
82          int ans, r;  
83          Divide(i, j, out ans, out r);  
84          Console.WriteLine("{0} / {1} = {2}r{3}", i, j, ans, r);  
85       }  
86    }  
87 }  
88 ]]></code_example>shows a Divide method that includes two output parameters-one for the result of the division and another for the remainder. </paragraph>
89   <paragraph>For value, reference, and output parameters, there is a one-to-one correspondence between caller-provided arguments and the parameters used to represent them. A parameter array enables a many-to-one relationship: many arguments can be represented by a single parameter array. In other words, parameter arrays enable variable length argument lists. </paragraph>
90   <paragraph>A parameter array is declared with a params modifier. There can be only one parameter array for a given method, and it must always be the last parameter specified. The type of a parameter array is always a single dimensional array type. A caller can either pass a single argument of this array type, or any number of arguments of the element type of this array type. For instance, the example <code_example><![CDATA[
91 using System;  
92 class Test  
93 {  
94    static void F(params int[] args) {  
95       Console.WriteLine("# of arguments: {0}", args.Length);  
96       for (int i = 0; i < args.Length; i++)  
97       Console.WriteLine("\targs[{0}] = {1}", i, args[i]);  
98    }  
99    static void Main() {  
100       F();  
101       F(1);  
102       F(1, 2);  
103       F(1, 2, 3);  
104       F(new int[] {1, 2, 3, 4});  
105    }  
106 }  
107 ]]></code_example>shows a method F that takes a variable number of <keyword>int</keyword> arguments, and several invocations of this method. The output is: <code_example><![CDATA[
108 # of arguments: 0  
109 # of arguments: 1  
110 args[0] = 1  
111 # of arguments: 2  
112 args[0] = 1  
113 args[1] = 2  
114 # of arguments: 3  
115 args[0] = 1  
116 args[1] = 2  
117 args[2] = 3  
118 # of arguments: 4  
119 args[0] = 1  
120 args[1] = 2  
121 args[2] = 3  
122 args[3] = 4  
123 ]]></code_example></paragraph>
124   <paragraph>Most of the examples presented in this introduction use the WriteLine method of the Console class. The argument substitution behavior of this method, as exhibited in the example <code_example><![CDATA[
125 int a = 1, b = 2;  
126 Console.WriteLine("a = {0}, b = {1}", a, b);  
127 ]]></code_example>is accomplished using a parameter array. The WriteLine method provides several overloaded methods for the common cases in which a small number of arguments are passed, and one method that uses a parameter array. <code_example><![CDATA[
128 namespace System  
129 {  
130    public class Console  
131    {  
132       public static void WriteLine(string s) {...}  
133       public static void WriteLine(string s, object a) {...}  
134       public static void WriteLine(string s, object a, object b) {...}  
135       ...  
136       public static void WriteLine(string s, params object[] args) {...}  
137    }  
138 }  
139 ]]></code_example></paragraph>
140 </clause>