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 ?
With the
paramskeyword.Here is an example:
The code shows various things. First of all the argument with the
paramskeyword must always be last (and there can be only one per function). Furthermore, you can call a function that takes aparamsargument in two ways. The first way is illustrated in the first line ofMyFunctionwhere each number is added as a single argument. However, it can also be called with an array as is illustrated inSumThemAllAndPrintInStringwhich callsSumThemAllwith theint[]callednumbers.