class Test
{
static void Main()
{
Foo("Hello");
}
static void Foo(object x)
{
Console.WriteLine("object");
}
static void Foo
{
Console.WriteLine("params T[]");
}
}
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Akkiraju IvaturiPosted Sep 7, 2012, 11:25 PM
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 thatTshould beSystem.String. Nothing scary so far.Then overloading tries to work out which method is "better". If it's a choice between
string xandparams string[] xthe former will always win - but at this point it's effectively a choice betweenobject xandparams 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 theparamsparameter is then used. This means that by the time actual conversions are considered, the choices areobject xorstring x- so clearly the latter wins.