Add a more functional (i.e. fewer-stubs) implementation of System.Data.Linq.
[mono.git] / mcs / docs / ecma334 / 17.5.1.4.xml
1 <?xml version="1.0"?>
2 <clause number="17.5.1.4" title="Parameter arrays">
3   <paragraph>A parameter declared with a params modifier is a parameter array. If a formal parameter list includes a parameter array, it must be the last parameter in the list and it must be of a single-dimensional array type. </paragraph>
4   <paragraph><example>[Example: For example, the types string[] and string[][] can be used as the type of a parameter array, but the type string[,] can not. end example]</example> It is not possible to combine the params modifier with the modifiers ref and out. </paragraph>
5   <paragraph>A parameter array permits arguments to be specified in one of two ways in a method invocation: <list><list_item> The argument given for a parameter array can be a single expression of a type that is implicitly convertible (<hyperlink>13.1</hyperlink>) to the parameter array type. In this case, the parameter array acts precisely like a value parameter. </list_item><list_item> Alternatively, the invocation can specify zero or more arguments for the parameter array, where each argument is an expression of a type that is implicitly convertible (<hyperlink>13.1</hyperlink>) to the element type of the parameter array. In this case, the invocation creates an instance of the parameter array type with a length corresponding to the number of arguments, initializes the elements of the array instance with the given argument values, and uses the newly created array instance as the actual argument. </list_item></list></paragraph>
6   <paragraph>Except for allowing a variable number of arguments in an invocation, a parameter array is precisely equivalent to a value parameter (<hyperlink>17.5.1.1</hyperlink>) of the same type. </paragraph>
7   <paragraph>
8     <example>[Example: The example <code_example><![CDATA[
9 using System;  
10 class Test  
11 {  
12    static void F(params int[] args) {  
13       Console.Write("Array contains {0} elements:", args.Length);  
14       foreach (int i in args)  
15       Console.Write(" {0}", i);  
16       Console.WriteLine();  
17    }  
18    static void Main() {  
19       int[] arr = {1, 2, 3};  
20       F(arr);  
21       F(10, 20, 30, 40);  
22       F();  
23    }  
24 }  
25 ]]></code_example>produces the output <code_example><![CDATA[
26 Array contains 3 elements: 1 2 3  
27 Array contains 4 elements: 10 20 30 40  
28 Array contains 0 elements:  
29 ]]></code_example></example>
30   </paragraph>
31   <paragraph>
32     <example>The first invocation of F simply passes the array a as a value parameter. The second invocation of F automatically creates a four-element int[] with the given element values and passes that array instance as a value parameter. Likewise, the third invocation of F creates a zero-element int[] and passes that instance as a value parameter. The second and third invocations are precisely equivalent to writing: <code_example><![CDATA[
33 F(new int[] {10, 20, 30, 40});  
34 F(new int[] {});  
35 ]]></code_example>end example]</example>
36   </paragraph>
37   <paragraph>When performing overload resolution, a method with a parameter array may be applicable either in its normal form or in its expanded form (<hyperlink>14.4.2.1</hyperlink>). The expanded form of a method is available only if the normal form of the method is not applicable and only if a method with the same signature as the expanded form is not already declared in the same type. </paragraph>
38   <paragraph>
39     <example>[Example: The example <code_example><![CDATA[
40 using System;  
41 class Test  
42 {  
43    static void F(params object[] a) {  
44       Console.WriteLine("F(object[])");  
45    }  
46    static void F() {  
47       Console.WriteLine("F()");  
48    }  
49    static void F(object a0, object a1) {  
50       Console.WriteLine("F(object,object)");  
51    }  
52    static void Main() {  
53       F();  
54       F(1);  
55       F(1, 2);  
56       F(1, 2, 3);  
57       F(1, 2, 3, 4);  
58    }  
59 }  
60 ]]></code_example>produces the output <code_example><![CDATA[
61 F();  
62 F(object[]);  
63 F(object,object);  
64 F(object[]);  
65 F(object[]);  
66 ]]></code_example></example>
67   </paragraph>
68   <paragraph>
69     <example>In the example, two of the possible expanded forms of the method with a parameter array are already included in the class as regular methods. These expanded forms are therefore not considered when performing overload resolution, and the first and third method invocations thus select the regular methods. </example>
70   </paragraph>
71   <paragraph>
72     <example>When a class declares a method with a parameter array, it is not uncommon to also include some of the expanded forms as regular methods. By doing so it is possible to avoid the allocation of an array instance that occurs when an expanded form of a method with a parameter array is invoked. end example]</example>
73   </paragraph>
74   <paragraph>When the type of a parameter array is object[], a potential ambiguity arises between the normal form of the method and the expended form for a single object parameter. The reason for the ambiguity is that an object[] is itself implicitly convertible to type object. The ambiguity presents no problem, however, since it can be resolved by inserting a cast if needed. </paragraph>
75   <paragraph>
76     <example>[Example: The example <code_example><![CDATA[
77 using System;  
78 class Test  
79 {  
80    static void F(params object[] args) {  
81       foreach (object o in args) {  
82          Console.Write(o.GetType().FullName);  
83          Console.Write(" ");  
84       }  
85       Console.WriteLine();  
86    }  
87    static void Main() {  
88       object[] a = {1, "Hello", 123.456};  
89       object o = a;  
90       F(a);  
91       F((object)a);  
92       F(o);  
93       F((object[])o);  
94    }  
95 }  
96 ]]></code_example>produces the output <code_example><![CDATA[
97 System.Int32 System.String System.Double  
98 System.Object[]  
99 System.Object[]  
100 System.Int32 System.String System.Double  
101 ]]></code_example></example>
102   </paragraph>
103   <paragraph>
104     <example>In the first and last invocations of F, the normal form of F is applicable because an implicit conversion exists from the argument type to the parameter type (both are of type object[]). Thus, overload resolution selects the normal form of F, and the argument is passed as a regular value parameter. In the second and third invocations, the normal form of F is not applicable because no implicit conversion exists from the argument type to the parameter type (type object cannot be implicitly converted to type object[]). However, the expanded form of F is applicable, so it is selected by overload resolution. As a result, a one-element object[] is created by the invocation, and the single element of the array is initialized with the given argument value (which itself is a reference to an object[]). end example]</example>
105   </paragraph>
106 </clause>