use the params feature to write functions which accept a variable number of arguments : params « Language Basics « C# / C Sharp






use the params feature to write functions which accept a variable number of arguments

 
using System;

public class MainClass {
    public static void Main(string[] args) {
        int nSum;
        Console.WriteLine("{0}", SumArgs(out nSum, 1, 2, 3));

        int[] nArray = { 4, 5, 6 };
        Console.WriteLine("{0}", SumArgs(out nSum, nArray));

    }

    public static int SumArgs(out int nSum, params int[] list) {
        nSum = 0;
        foreach (int n in list) {
            nSum += n;
        }
        return nSum;
    }
}

 








Related examples in the same category

1.Params Array
2.Normal parameter and params parameters
3.Check the array length for params parameters
4.Use params to mark parameter