58
votes

In C# you can do this:

foo = string.Format("{0} {1} {2} {3} ...", "aa", "bb", "cc" ...);

This method Format() accepts infinite parameters, being the first one how the string should be formatted and the rest are values to be put in the string.

Today I've come to a situation where I had to get a set of strings and test them, then I remembered this language functionality, but I had no clue. After a few unsuccessful web searches, I've realised it would be more prudent to just get an array, which didn't make me quite satisfied.

Q: How do I make a function that accepts infinite parameters? And how do I use it ?

8
infinite...that's a lot of parameters. Have you tried it out? - flq
Pedant point, it isn't "infinite" by any means; but any sane code should be OK. - Marc Gravell
I would be surprised if anyone had infinite memory. I want some! - Callum Rogers
@Christian.K I think it's more clarifying like this, since it was my original question and might be more easily linked with similar doubts others might get. And (in a general way) this is a virtual way of creating overloads (if you put one of them wrong, visual studio complains by saying "none of the overloads accept these parameters" or something like this) - Marcelo
@MarceloRamires: I was trying to refer to the fact, that the "params" keyword, which is what the example you have given is all about, has as much todo with overloads as has any other parameterlist of a method, but rather allows a method to be called with an array of "infinite items". But seriously, it is your question, so by all means keep it like that if the title is stating what you intended to know/ask (I may have misunderstood then, fair enough :-) - Christian.K

8 Answers

102
votes

With the params keyword.

Here is an example:

    public int SumThemAll(params int[] numbers)
    {
        return numbers.Sum();
    }

    public void SumThemAllAndPrintInString(string s, params int[] numbers)
    {
        Console.WriteLine(string.Format(s, SumThemAll(numbers)));
    }

    public void MyFunction()
    {
        int result = SumThemAll(2, 3, 4, 42);
        SumThemAllAndPrintInString("The result is: {0}", 1, 2, 3);
    }

The code shows various things. First of all the argument with the params keyword must always be last (and there can be only one per function). Furthermore, you can call a function that takes a params argument in two ways. The first way is illustrated in the first line of MyFunction where each number is added as a single argument. However, it can also be called with an array as is illustrated in SumThemAllAndPrintInString which calls SumThemAll with the int[] called numbers.

22
votes

Use the params keyword. Usage:

public void DoSomething(int someValue, params string[] values)
{
    foreach (string value in values)
        Console.WriteLine(value);
}

The parameter that uses the params keyword always comes at the end.

6
votes

A few notes.

Params needs to be marked on an array type, like string[] or object[].

The parameter marked w/ params has to be the last argument of your method. Foo(string input1, object[] items) for example.

3
votes

use the params keyword. For example

static void Main(params string[] args)
{
    foreach (string arg in args)
    {
        Console.WriteLine(arg);
    }
}
3
votes

You can achieve this by using the params keyword.

Little example:

public void AddItems(params string[] items)
{
     foreach (string item in items)
     { 
         // Do Your Magic
     }
}
3
votes
    public static void TestStrings(params string[] stringsList)
    {
        foreach (string s in stringsList){ } 
            // your logic here
    }
1
votes
 public string Format(params string[] value)
 {
            // implementation
 }

The params keyword is used

1
votes
function void MyFunction(string format, params object[] parameters) {

}

Instad of object[] you can use any type your like. The params argument always has to be the last in the line.