Skip to content

Type inference a-go-go

Akkiraju Ivaturi

Akkiraju Ivaturi

Asked Sep 7, 2012, 7:25 PM EDT1 answer
Share
LinkedInX
using System;

class Test
{
static void Main()
{
Foo("Hello");
}

static void Foo(object x)
{
Console.WriteLine("object");
}

static void Foo<T>(params T[] x)
{
Console.WriteLine("params T[]");
}
} 

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Akkiraju Ivaturi
    0

    params T[] is printed. Now why would the compiler choose to create an array when it doesn't have to? Well... there are two stages to this. Firstly, when trying to find overloads which are legitimate candidates to be called, type inference works out that T should be System.String. Nothing scary so far.

    Then overloading tries to work out which method is "better". If it's a choice between string x and params string[] x the former will always win - but at this point it's effectively a choice between object x and params string[] x. (The fact that one is actually a generic method is only relevant in a tie-break situation.) For the purposes of working out "better conversions" the expanded form of the method with the params parameter is then used. This means that by the time actual conversions are considered, the choices are object x or string x - so clearly the latter wins. 

    0