Interviews2 answers
Create a function in C# which can accept varying number of arguments?
2 Answers
// function containing params parameters
public static int Add(params int[] ListNumbers)
{
int total = 0;
// foreach loop
foreach(int i in ListNumbers)
{
total = i;
}
return total;
}
Naeem KhanPosted Nov 27, 2022, 5:21 AM EST
Hi Brahma Prakash Shukla,
“params“ can be used to create a function in C# which can accept varying number of arguments as like following:
void PrintReport(string header, params int[] numbers){Console.WriteLine(header);foreach (int number in numbers)Console.WriteLine(number);}
For more details:
Function with variable number of arguments
https://stackoverflow.com/questions/9784630/function-with-variable-number-of-arguments
Hope, this will help you!
Pankajkumar PatelPosted Nov 17, 2022, 2:43 AM EST


