X hits on this document

1415 views

0 shares

0 downloads

0 comments

235 / 396

C# LANGUAGE SPECIFICATION

static void Main() { F(); F(1); F(1, 2); F(1, 2, 3); F(1, 2, 3, 4); } }

produces the output

F(); F(object[]); F(object,object); F(object[]); F(object[]);

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. 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.

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.

The example

using System;

class Test { static void F(params object[] args) { foreach (object o in args) { Console.Write(o.GetType().FullName); Console.Write(" "); } Console.WriteLine(); }

static void Main() { object[] a = {1, "Hello", 123.456}; object o = a; F(a); F((object)a); F(o); F((object[])o); } }

produces the output

System.Int32 System.String System.Double System.Object[] System.Object[] System.Int32 System.String System.Double

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

Document info
Document views1415
Page views1415
Page last viewedMon Jan 23 09:23:42 UTC 2017
Pages396
Paragraphs9401
Words133190

Comments