robertpas
robertpas

Reputation: 673

C# Calling a function with multiple values

So I have this code here :

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 me to call the Calculate(n,time) function for multiple n values (given one after the other), but the same time. I already thought of using an array to store multiple n values, but is there a better option.

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

Any ideas? Thanks in advance!

Upvotes: 5

Views: 330

Answers (3)

Alexander
Alexander

Reputation: 11

// 
private void Calculate(int int_value, param int[] int_array_value)
{
`   enter code here`// your code goes here
}

Upvotes: 1

Dave New
Dave New

Reputation: 40092

An array should work perfectly.

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

Also with LINQ you can perform a distinct selection of your array:

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

Upvotes: 0

Goran Žuri
Goran Žuri

Reputation: 1643

You just use params attribute.

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

This would allow you to call:

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

In function you can iterate:

foreach(int item in parameters){}

Upvotes: 7

Related Questions