X hits on this document





234 / 396

Chapter ‎18   Unsafe code

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 output

Array contains 3 elements: 1 2 3 Array contains 4 elements: 10 20 30 40 Array contains 0 elements:

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:

F(new int[] {10, 20, 30, 40}); F(new int[] {});

When performing overload resolution, a method with a parameter array may be applicable either in its normal form or in its expanded form (§‎ 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.

The example

using System;

class Test { static void F(params object[] a) { Console.WriteLine("F(object[])"); }

static void F() { Console.WriteLine("F()"); }

static void F(object a0, object a1) { Console.WriteLine("F(object,object)"); }

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

Document info
Document views577
Page views577
Page last viewedTue Oct 25 14:42:49 UTC 2016