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
  • 732,580
  • 175
  • 1,330
  • 1,459

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,093
  • 3
  • 22
  • 43
0

Use params keyword:

int foo(params int[] arguments)
{
....
}
Abdellah OUMGHAR
  • 3,627
  • 1
  • 11
  • 16
Dr.Haimovitz
  • 1,568
  • 12
  • 16
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
  • 575
  • 4
  • 14