13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 14 Expressions• For a value parameter, the argument expression is evaluated and an implicit conversion (§13.1) to thecorresponding parameter type is performed. The resulting value becomes the initial value of the valueparameter in the function member invocation.• For a reference or output parameter, the variable reference is evaluated and the resulting storage locationbecomes the storage location represented by the parameter in the function member invocation. If thevariable reference given as a reference or output parameter is an array element of a reference-type, arun-time check is performed to ensure that the element type of the array is identical to the type of theparameter. If this check fails, a System.ArrayTypeMismatchException is thrown.Methods, indexers, and instance constructors may declare their right-most parameter to be a parameter array(§17.5.1.4). Such function members are invoked either in their normal form or in their expanded formdepending on which is applicable (§14.4.2.1):• When a function member with a parameter array is invoked in its normal form, the argument given forthe parameter array must be a single expression of a type that is implicitly convertible (§13.1) to theparameter array type. In this case, the parameter array acts precisely like a value parameter.• When a function member with a parameter array is invoked in its expanded form, the invocation mustspecify zero or more arguments for the parameter array, where each argument is an expression of a typethat is implicitly convertible (§13.1) to the element type of the parameter array. In this case, theinvocation creates an instance of the parameter array type with a length corresponding to the number ofarguments, initializes the elements of the array instance with the given argument values, and uses thenewly created array instance as the actual argument.The expressions of an argument list are always evaluated in the order they are written. [Example: Thus, theexampleclass Test{static void F(int x, int y, int z) {System.Console.WriteLine("x = {0}, y = {1}, z = {2}", x, y, z);}static void Main() {int i = 0;F(i++, i++, i++);}}produces the outputx = 0, y = 1, z = 2end example]The array covariance rules (§19.5) permit a value of an array type A[] to be a reference to an instance of anarray type B[], provided an implicit reference conversion exists from B to A. Because of these rules, whenan array element of a reference-type is passed as a reference or output parameter, a run-time check isrequired to ensure that the actual element type of the array is identical to that of the parameter. [Example: Inthe exampleclass Test{static void F(ref object x) {…}static void Main() {object[] a = new object[10];object[] b = new string[10];F(ref a[0]); // OkF(ref b[1]); // ArrayTypeMismatchException}}the second invocation of F causes a System.ArrayTypeMismatchException to be thrown because theactual element type of b is string and not object. end example]133

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

Saved successfully!

Ooh no, something went wrong!