88

As the title says I need to know if there is a corresponding syntax as java's ... in method parameters, like

void printReport(String header, int... numbers) { //numbers represents varargs
  System.out.println(header);
  for (int num : numbers) {
     System.out.println(num);
  }
}

(code courtesy of wikipedia)

Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
Gabber
  • 5,152
  • 6
  • 35
  • 49
  • 2
    duplicate question.please read this link.http://stackoverflow.com/questions/910585/c-sharp-variable-length-args-which-is-better-and-why-arglist-params-array-o or for example read this http://msdn.microsoft.com/en-us/library/ms182366(v=vs.80).aspx – Rakesh Patel Mar 20 '12 at 10:06
  • 1
    Can't find the parallel with java in your link – Gabber Mar 20 '12 at 12:25

7 Answers7

121

Yes you can write something like this:

void PrintReport(string header, params int[] numbers)
{
    Console.WriteLine(header);
    foreach (int number in numbers)
        Console.WriteLine(number);
}
Tudor
  • 61,523
  • 12
  • 102
  • 142
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
23

Try using the params keyword, placed before the statement, eg

myFunction(params int[] numbers);
Marius
  • 3,043
  • 1
  • 15
  • 24
16

Yes, there is. As Adriano said you can use C# 'params' keyword. An example is the in link below:

params (C# Reference)

http://msdn.microsoft.com/en-us/library/w5zay9db.aspx

"The params keyword lets you specify a method parameter that takes a variable number of arguments.

You can send a comma-separated list of arguments of the type specified in the parameter declaration, or an array of arguments of the specified type. You also can send no arguments.

No additional parameters are permitted after the params keyword in a method declaration, and only one params keyword is permitted in a method declaration."

Carlos Quintanilla
  • 12,937
  • 3
  • 22
  • 25
6

You can declare a method to har a variable number of parameters by using the params keyword. Just like when using ... in Java, this will give you an array and let you call the metods with a variable number of parameters: http://msdn.microsoft.com/en-us/library/w5zay9db(v=vs.71).aspx

Mathias Schwarz
  • 7,099
  • 23
  • 28
5

This should be

void printReport(String header, params int[] numbers)
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
4

I believe you mean params

public void printReport(string header, params int[] list) 
{
    Console.WriteLine(header);

    for (int i = 0 ; i < list.Length; i++)
    {
        Console.WriteLine(list[i]);
    }
    Console.WriteLine();
}
Tokk
  • 4,499
  • 2
  • 31
  • 47
0

You can use params, although this must always come last in the list:

public void PrintReport(string header, params int[] numbers)
{

It is however possible to combine params optional parameters (such as [CallerMemberName]) by using named arguments, which works even if the parameters are of the same type.

Declare the method like this:

public static void PrintReport(
           [CallerMemberName] string callerName = "",
           [CallerFilePath] string sourceFilePath = "", 
           params string[] inputStrings)
{

and call it like this:

PrintReport(inputStrings: new[] { "string 1", "string 2" } );
SharpC
  • 6,974
  • 4
  • 45
  • 40