1

Is there a way in c# to declare a function that can take a dynamic number of same type arguments without overloading the function like:

in foo(...)

foo(1) foo(1, 2) foo(1, 2, 3...)
marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425

3 Answers3

1

You need to read about params.

You can specify a method's signature such as:

public void Foo(params int[] list)
{
}

Where list is going to be an array of integers.

Nikola
  • 2,033
  • 3
  • 21
  • 42
0

Use params keyword:

int foo(params int[] arguments)
{
....
}
Abdellah OUMGHAR
  • 3,494
  • 1
  • 10
  • 16
Dr.Haimovitz
  • 1,508
  • 10
  • 15
0

Using the ParamArrayAttribute you can get the desired effect.

Public void Foo(params int[] list){}

Examples of calling the method:

Foo(1); Foo(1,2); Foo(1,2,3);

Please see here for more info on Msdn multiple parameters

doodlleus
  • 555
  • 3
  • 13