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!

Answers


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){}

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

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);

Need Your Help

Remove leading whitespace in Mako template

templates whitespace mako removing-whitespace

In a use case for autogenerating code from a Mako template, I'd like to have a nice syntax to remove leading whitespace (similar to removing newlines with \ at the end of a line).

Rails: how to use options correctly for bundle install

ruby-on-rails ruby rubygems bundler config

I'm just tried to create a new rails app on a server running rails 3.2.5 (ruby 1.8.7), using a mysql backend: