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 17 Classes17.5.1.4 Parameter arraysA parameter declared with a params modifier is a parameter array. If a formal parameter list includes aparameter array, it must be the last parameter in the list and it must be of a single-dimensional array type.[Example: For example, the types string[] and string[][] can be used as the type of a parameter array, butthe type string[,] can not. end example] It is not possible to combine the params modifier with the modifiersref and out.A parameter array permits arguments to be specified in one of two ways in a method invocation:• The argument given for a parameter array can be a single expression of a type that is implicitly convertible(§13.1) to the parameter array type. In this case, the parameter array acts precisely like a value parameter.• Alternatively, the invocation can specify zero or more arguments for the parameter array, where eachargument is an expression of a type that is implicitly convertible (§13.1) to the element type of the parameterarray. In this case, the invocation creates an instance of the parameter array type with a length correspondingto the number of arguments, initializes the elements of the array instance with the given argument values, anduses the newly created array instance as the actual argument.Except for allowing a variable number of arguments in an invocation, a parameter array is precisely equivalent toa value parameter (§17.5.1.1) of the same type.[Example: The exampleusing System;class Test{static void F(params int[] args) {Console.Write("Array contains {0} elements:", args.Length);foreach (int i in args)Console.Write(" {0}", i);Console.WriteLine();}static void Main() {int[] arr = {1, 2, 3};F(arr);F(10, 20, 30, 40);F();}}produces the outputArray contains 3 elements: 1 2 3Array contains 4 elements: 10 20 30 40Array contains 0 elements:The first invocation of F simply passes the array a as a value parameter. The second invocation of Fautomatically creates a four-element int[] with the given element values and passes that array instance as avalue parameter. Likewise, the third invocation of F creates a zero-element int[] and passes that instance as avalue parameter. The second and third invocations are precisely equivalent to writing:F(new int[] {10, 20, 30, 40});F(new int[] {});end example]When performing overload resolution, a method with a parameter array may be applicable either in its normalform or in its expanded form (§14.4.2.1). The expanded form of a method is available only if the normal form ofthe method is not applicable and only if a method with the same signature as the expanded form is not alreadydeclared in the same type.[Example: The example231

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

Saved successfully!

Ooh no, something went wrong!