Nick
Nick

Reputation: 110

How to pass a dynamic number of arguments into a function

I have a situation where I want to pass an unknown number of arguments to a function. The function is basically structured as:

public void test(string x, params string[] y)
{
    //code
}

I need to pass an unknown number of arguments into the y input. In some cases, I may have 2 y's, in others I could have 20 y's in the format of

test("test", arg[0], arg[1], arg[2], arg[3] ... arg[20]);

Any suggestions on how to enter a varying number of y inputs into the above function? I'm attempting to automate a process and c# is new to me.

Upvotes: 0

Views: 561

Answers (2)

Randall Sandoval
Randall Sandoval

Reputation: 247

why you don't use a list, you can add the elements to the list and then send it to your function see that link http://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx

Upvotes: 0

amiry jd
amiry jd

Reputation: 27585

You just need to pass arg:

test("test", arg);

Upvotes: 4

Related Questions