Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Calling a function with multiple values

Tags:

function

c#

call

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!

like image 749
robertpas Avatar asked Aug 01 '12 08:08

robertpas


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


1 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){}
like image 73
Goran Žuri Avatar answered Sep 28 '22 08:09

Goran Žuri