언어/[C#]

[C#] params 키워드

조랩 2023. 3. 12. 17:52
using System;
using System.Diagnostics.CodeAnalysis;
using System.Numerics;

namespace MyFirstProgram
{
    internal class Program
    {
        static void Main(string[] args) // main method
        {

            // params keyword: 같은 타입의 매개변수가 몇 개나 들어올지 모를 때 사용할 수 있음
            // 여러개의 매개변수를 하나로 퉁쳐서 함수 작성 가능

            double total1, total2;

            total1 = CheckOut(2, 3, 4, 5, 6, 7, 8);
            total2 = CheckOut(2, 3, 4, 5);

            Console.WriteLine(total1);
            Console.WriteLine(total2);
        }

        static double CheckOut(params double[] prices)
        {
            double total = 0;

            foreach (double price in prices)
            {
                total += price;
            }

            return total;
        }

    }
}
728x90