Question

How does c# make params work.

When you have code like

 static string Concat(int arg)
 {
     return arg.ToString();
 }

static string Concat(int arg, params string[] p)
{
    string result = string.Empty;
    result = result + arg.ToString();
    foreach (var s in p)
    {
        result = result + s;
    }
    return result;
}

Resharper says the method with params hides the other method. Does this imply that under the hood The other method is being defined and calling the one with params passing in an empty array? Or something else entirely?

(please don't get bogged down on the crappy code - its just an example)

Edit to explain:

If i do not define the static string Concat(int arg) method i can still call the second as

var s = Concat(123);

despite that method not being defined .... so the question is: is the first method defined implicitly under the hood and calling the second with an empty array. Note i changed the arg type from string to int.

to explain the weirdness as i see it. When you call as above ... it hits the params object with an empty array, the param is not null - which is what i expected. When i first wrote that foreach loop i had a null check on params then discovered i didn't need it.

So ... you reason that it must be wrapping any params after the first parameter in an array right? A little bit of inlined code? ... so if you passed in "dave" you would get freddave as the output ....

However pass null into it ....

static void Main(string[] args)
{
    Console.WriteLine(Concat("fred",null));
}

 //static string Concat(string arg)
 //{
 //    return arg;
 //}

static string Concat(string arg, params int[] p)
{
    string result = string.Empty;
    result = result + arg;
    if (p != null)
    {
        foreach (var s in p)
        {
            result = result + s.ToString();
        }
    }

    return result;
}

and you discover in this case p is actually null ... If you do that with the string example ... id of expected an array with 1 element of null ... instead the entire params object is null.

Was it helpful?

Solution

I think calling it "hiding" is misleading here, to be honest.

Both methods are definitely created, but if you call:

Concat(5);

then for that invocation both methods are applicable (in the terminology of the C# spec section 7.5.3.1) but the first method is better (section 7.5.3.2) because of this:

  • Otherwise, if MP is applicable in its normal form and MQ has a params array and is applicable only in its expanded form, then MP is better than MQ.

So in that case, the first method would be called.

The second method cannot be called using params expansion without specifying at least one string. It can still be called with an empty array, of course:

Concat(5, new string[0]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top