13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>C#</strong> LANGUAGE SPECIFICATIONusing 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)");}static void Main() {F();F(1);F(1, 2);F(1, 2, 3);F(1, 2, 3, 4);}}produces the outputF();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 inthe class as regular methods. These expanded forms are therefore not considered when performing overloadresolution, and the first and third method invocations thus select the regular methods. When a class declares amethod with a parameter array, it is not uncommon to also include some of the expanded forms as regularmethods. By doing so it is possible to avoid the allocation of an array instance that occurs when an expandedform of a method with a parameter array is invoked. end example]When the type of a parameter array is object[], a potential ambiguity arises between the normal form of themethod 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 beresolved by inserting a cast if needed.[Example: The exampleusing 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 output232

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

Saved successfully!

Ooh no, something went wrong!