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()
?
If your problem is just to add another element to your array, you could use a List
List<object> list = new List<object> { "hello" };
list.AddRange(objs);
Console.WriteLine("{0}, {1}, {2}, ...", list.ToArray());
params
is not a datatype. The parameters datatype is just still a plain array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With