params(C# 참조)
업데이트: 2007년 11월
params 키워드를 사용하면 인수 개수가 변수로 주어지는 인수를 사용하는 메서드 매개 변수를 지정할 수 있습니다.
메서드 선언에서 params 키워드 다음에는 매개 변수를 추가할 수 없으며 params 키워드 하나만 메서드 선언에 사용할 수 있습니다.
예제
public class MyClass
{
public static void UseParams(params int[] list)
{
for (int i = 0 ; i < list.Length; i++)
{
Console.Write(list[i] + " ");
}
Console.WriteLine();
}
public static void UseParams2(params object[] list)
{
for (int i = 0 ; i < list.Length; i++)
{
Console.Write(list[i] + " ");
}
Console.WriteLine();
}
static void Main()
{
UseParams(1, 2, 3);
UseParams2(1, 'a', "test");
// An array of objects can also be passed, as long as
// the array type matches the method being called.
int[] myarray = new int[3] {10,11,12};
UseParams(myarray);
}
}
/*
Output:
1 2 3
1 a test
10 11 12
*/
C# 언어 사양
자세한 내용은 C# 언어 사양의 다음 단원을 참조하십시오.
- 10.6.1.4 매개 변수 배열