X hits on this document

747 views

0 shares

0 downloads

0 comments

129 / 396

C# LANGUAGE SPECIFICATION

For a reference or output parameter, the variable reference is evaluated and the resulting storage location becomes the storage location represented by the parameter in the function member invocation. If the variable reference given as a reference or output parameter is an array element of a reference-type, a run-time check is performed to ensure that the element type of the array is identical to the type of the parameter. 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 (§‎10.5.1.4). Such function members are invoked either in their normal form or in their expanded form depending on which is applicable (§‎7.4.2.1):

When a function member with a parameter array is invoked in its normal form, the argument given for the parameter array must be a single expression of a type that is implicitly convertible (§‎6.1) to the parameter 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 must specify zero or more arguments for the parameter array, where each argument is an expression of a type that is implicitly convertible (§‎6.1) 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.

The expressions of an argument list are always evaluated in the order they are written. Thus, the example

class 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 output

x = 0, y = 1, z = 2

The array co-variance rules (§‎12.5) permit a value of an array type A[] to be a reference to an instance of an array type B[], provided an implicit reference conversion exists from B to A. Because of these rules, when an array element of a reference-type is passed as a reference or output parameter, a run-time check is required to ensure that the actual element type of the array is identical to that of the parameter. In the example

class Test { static void F(ref object x) {...}

static void Main() { object[] a = new object[10]; object[] b = new string[10]; F(ref a[0]);// Ok F(ref b[1]);// ArrayTypeMismatchException } }

the second invocation of F causes a System.ArrayTypeMismatchException to be thrown because the actual element type of b is string and not object.

116Copyright Microsoft Corporation 1999-2003. All Rights Reserved.

Document info
Document views747
Page views747
Page last viewedSat Oct 29 00:13:22 UTC 2016
Pages396
Paragraphs9401
Words133190

Comments