Using variable-length argument lists. - CSharp Custom Type

CSharp examples for Custom Type:Method Parameter

Description

Using variable-length argument lists.

Demo Code



using System;// w  w w  .  j  av  a2  s .  c  o m

class test
{
    static double Average(params double[] numbers)
    {
        var total = 0.0; // initialize total

        // calculate total using the foreach statement
        foreach (var d in numbers)
        {
            total += d;
        }

        return numbers.Length != 0 ? total / numbers.Length : 0.0;
    }

    public static void Main()
    {
        var d1 = 1.0;
        var d2 = 2.0;
        var d3 = 3.0;
        var d4 = 4.0;

        Console.WriteLine($"d1 = {d1:F1}\nd2 = {d2:F1}\nd3 = {d3:F1}\nd4 = {d4:F1}\n");
        Console.WriteLine($"Average of d1 and d2 is {Average(d1, d2):F1}");
        Console.WriteLine($"Average of d1, d2 and d3 is {Average(d1, d2, d3):F1}");
        Console.WriteLine($"Average of d1, d2, d3 and d4 is {Average(d1, d2, d3, d4):F1}");
    }
}

Result


Related Tutorials