유니티

[C#] 가변 인수

simstealer 2022. 9. 15. 13:20

- 인수의 개수가 유연하게 변할 수 있는 인수를 말합니다.

using static System.Console;
using System.Collections;
using System;

namespace Practice01
{
    class Class1
    {
        static void Main()
        {
            int one = Boo(1, 2, 3, 4, 5);
            Console.WriteLine(one);
            int two = Boo(1, 2, 3, 4, 5, 6, 7, 8, 9);
            Console.WriteLine(two);
        }

        static int Boo(params int[] arr)
        {
            int sum = 0;
            for (int i = 0; i < arr.Length; i++)
            {
                sum += arr[i];
            }
            return sum;
        }
    }
}