Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating methods with infinite parameters?

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 ?

like image 537
Marcelo Avatar asked Mar 12 '10 20:03

Marcelo


People also ask

How do you pass an infinite argument in Java?

It's called varargs. It allows a method to take any number of arguments. They are accessible as an array in the method: public void foo(String...

How many types of parameters method can have?

Out Parameters. Default Parameters or Optional Arguments (C# 4.0 and above) Dynamic parameter (dynamic keyword). Value parameter or Passing Value Types by Value (normal C# method param are value parameter)

What is the method of parameter?

Parameters and Arguments Information can be passed to methods as parameter. Parameters act as variables inside the method. Parameters are specified after the method name, inside the parentheses.

What is method argument in Java?

Note: Parameters refers to the list of variables in a method declaration. Arguments are the actual values that are passed in when the method is invoked. When you invoke a method, the arguments used must match the declaration's parameters in type and order.


2 Answers

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.

like image 133
Klaus Byskov Pedersen Avatar answered Sep 21 '22 15:09

Klaus Byskov Pedersen


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.

like image 32
David Morton Avatar answered Sep 19 '22 15:09

David Morton