Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I forward/delegate a params list to another method?

Can I forward a params parameter to another method?

e.g.,

void MyStringMethod(string format, params object[] list)
{
  String.Format(format, list);
} 
like image 836
User Avatar asked Jul 27 '12 01:07

User


4 Answers

Works for me.

void Main()
{
    Method1("{0},{1},{2},{3}","Can","I","do","this").Dump();
}

String Method1(string format, params object[] list)
{
    return String.Format(format,list);
}

returns

Can,I,do,this
like image 169
Bert Avatar answered Nov 06 '22 16:11

Bert


yes ... but!

I know this is technically not a different answer, however I've been burned so many times with really hard to debug errors (caused by using params in what looked like really simple scenarios) that I feel compelled to post this comment.

Please be very careful when you use params. Ideally don't use them, unless you feel you absolutely have to, and then make sure you don't use the same data type in front of the params! This can make quickly reading the code (scanning) it and knowing what it will do very difficult, or worse, give you an unexpected and hard to find bug.

For example, in the code below, it's not easy to see what will be printed to the Console. Is the (1,2,3) being passed to WhoKnocked actually being identified as int 1 followed by int[] [2,3] or perhaps int[] [1,2,3]?

void Main()
{
    Console.WriteLine(WhoKnocked(1,2,3));
}

public string WhoKnocked(int x, params int[] knocks)
{
    return "It's mee!";
}

public string WhoKnocked(params int[] knocks)
{
    return "No, it's not, its you!";
}

lastly, here's another example that might give you some surprising results.

void Main()
{
    Greet(1,"foo", "bar");
    Greet(1, 2, "bar");
    Greet(1,"foo", new object());
    Greet(1,2,3);
    Greet(1,2,3,4);
}



public void Greet(int i, params object[] foo)
{
    Console.WriteLine("Number then param array of objects!");
}

public void Greet(int i, int x, params object[] foo)
{
    Console.WriteLine("Number, ...nuther number, and finally object[]!");
}

public void Greet(int i, string x, params object[] foo)
{
    Console.WriteLine("number, then string, then object[]!");
}

produces the following output

Number, then String, then Object[]!
Number, ...nuther Number, and finally Object[]!
Number, then String, then Object[]!
Number, ...nuther Number, and finally Object[]!
Number, ...nuther Number, and finally Object[]!
like image 3
snowcode Avatar answered Nov 06 '22 16:11

snowcode


The params keyword is just a form of syntactic sugar designed to allow you to make method calls as if they had a dynamic parameter count. All it is really is just a compiler transformation of multiple arguments to an array instantiation. That's all it is, an array.

An array is just another object that could be passed to other methods and whatnot so yes, you can forward that array of you wish.

like image 1
Jeff Mercado Avatar answered Nov 06 '22 17:11

Jeff Mercado


I believe you can, it is just an array of objects. If you then call another function that expects a param list then you can get unexpected results (depending on what you expect of course:-). Notice in the third case you only get 2 params.

    void Test()
    {
        DoIt(1, 2, 3, 4);
    }

    private void DoIt(params object[] p)
    {
        Console.WriteLine(p.Length);
        DoIt2(p);
        DoIt2(p, 5);
    }

    private void DoIt2(params object[] p)
    {
        Console.WriteLine(p.Length);
    }
like image 1
MikeKulls Avatar answered Nov 06 '22 18:11

MikeKulls