C # Calling a function with multiple values

So, I have this code:

int n;
public static void Main(string[] args)
    {
        Console.Write("Please insert a number : ");
        n = int.Parse(Console.ReadLine());
        Console.Write("Please insert wait time (0,1 or 2) : ");
        int time = int.Parse(Console.ReadLine())*1000;            
        Calculate(n,time);                      
    }

What is the best method for calling the Calculate (n, time) function for several n values ​​(specified one after the other), but at the same time. I already thought about using an array to store several n values, but there is a better option.

Also I would like to pass multiple n as arguments from the command line.

Any ideas? Thanks in advance!

+5
source share
3 answers

You just use the params attribute.

public void Calculate(time, params int[] parameters){ ... }

This will allow you to call:

Calculate(time, 1, 2, 3, 4, 5, 6, ....)

In a function, you can iterate:

foreach(int item in parameters){}
+7
source
// 
private void Calculate(int int_value, param int[] int_array_value)
{
`   enter code here`// your code goes here
}
+1
source

The array should work fine.

public void Calculate(int[] numbers, int time)
{ ... }

Also with LINQ you can make a separate selection of your array:

Calculate(n.Distinct().ToArray(), time);
0
source

All Articles