2
Reply

Create a function in C# which can accept varying number of arguments?

Create a function in C# which can accept varying number of arguments?

    Hi Brahma Prakash Shukla,

    params“ can be used to create a function in C# which can accept varying number of arguments as like following:

    1. void PrintReport(string header, params int[] numbers)
    2. {
    3. Console.WriteLine(header);
    4. foreach (int number in numbers)
    5. Console.WriteLine(number);
    6. }

    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!

    // function containing params parameterspublic static int Add(params int[] ListNumbers){int total = 0;// foreach loopforeach(int i in ListNumbers) {total = i;}return total;}