C#: Convert array to use in params with additional parameters

Posted by user1805759 on Stack Overflow See other posts from Stack Overflow or by user1805759
Published on 2012-11-07T10:50:56Z Indexed on 2012/11/07 11:00 UTC
Read the original article Hit count: 194

Filed under:
|
|

I have a method that takes params. Inside the method another variable shall be added to the output:

private void ParamsTest(params object[] objs)
{
  var foo = "hello";
  // Invalid: Interpretes objs as single array parameter:
  Console.WriteLine("{0}, {1}, {2}", foo, objs);
}

When I call

ParamsTest("Hi", "Ho");

I would like to see the output.

hello Hi Ho

What do I need to do? I can copy foo and objs into a new array and pass that array to WriteLine but is there a more elegant way to force objs to behave as params again? Kind of objs.ToParams()?

© Stack Overflow or respective owner

Related posts about c#

Related posts about arrays