14

Is it possible to send a variable number of arguments to a method?

For instance if I want to write a method that would concatenate many string[] objects into one string, but I wanted it to be able to accept arguments without knowing how many I would want to pass in, how would I do this?

5
  • Why wouldn't you pass an IEnumerable? Commented Feb 14, 2011 at 17:44
  • That infinite data structure is going to take a while to iterate over... Commented Feb 14, 2011 at 17:46
  • The title should probably say variable as opposed to infinite. Commented Feb 14, 2011 at 17:49
  • @David: Maybe you shouldn't be cracking jokes then. :P Commented Feb 14, 2011 at 17:53
  • @JohnOpincar I could have used IEnumerable, and it did cross my mind however in circumstances where I have multiple string[] objects I would have to write code to assign them to an IEnumerable object, so by using the params keyword (thanks for the tip everyone) I save a few lines. Commented Feb 14, 2011 at 21:01

9 Answers 9

21

You would do this as:

string ConcatString(params string[] arguments)
{
   // Do work here
}

This can be called as:

string result = ConcatString("Foo", "Bar", "Baz");

For details, see params (C# Reference).


FYI - There is already a String.Concat(params object[] args) - it will concatenate any set of objects by calling ToString() on each. So for this specific example, this is probably not really that useful.

Sign up to request clarification or add additional context in comments.

Comments

4

You can easily create a method that takes an arbitrary number of arguments (including zero), using the params keyword:

public static void UseParams(params int[] list)
{
    for (int i = 0; i < list.Length; i++)
        Console.Write(list[i] + " ");
}

You can even pass an array to this method instead of a list of parameters, and the method will work exactly the same.

Comments

4

Use the params keyword to do that:

public static string ConvertToOneString(params string[] list) 
{

    string result = String.Empty;
    for ( int i = 0 ; i < list.Length ; i++ )
    {
        result += list[i];
    }
    return result;
}

Usage:

string s = "hello"
string a = "world"
string result = ConvertToOneString(s, a);

Comments

3

very easy to do so using params keyword

void ConcatString(params String[] strArray)
{ 
    foreach (String s in strArray)
    {
        // do whatever you want with all the arguments here...
    }
}

Comments

2

yes, you can use params for that

Comments

2

Use params:

void YourMethod(params string[] infiniteArgs) { }

Comments

2
string MyConcat(params string[] values)
{
    var s = new StringBuilder();
    foreach (var v in values)
       s.Append(v);
    return s.ToString();
}

Comments

1

You don't mean infinite (the answer to that question is no) but you do mean 'variable'. Here is your answer (yes): http://blogs.msdn.com/b/csharpfaq/archive/2004/08/05/209384.aspx

1 Comment

Why the downvote? Perhaps the voter didn't see the history of the question. If so, please review and remove the downvote if you want to.
0

In practice, infinite - no (there is a finite amount of memory available, for example).

Unknown at design-time, yes.

The classic example of this is how it's handled on a Console application.

http://dotnetperls.com/main

http://msdn.microsoft.com/en-us/library/cb20e19t(VS.71).aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.