[C#] params 키워드

  • Post author:
  • Post category:
  • Post comments:0 Comments
  • Post last modified:December 8, 2004

params 키워드를 사용하면 인수 갯수가 변수로 주어지는 인수를 사용하는 메서드 매개 변수를 지정할 수 있습니다.

메서드 선언에서 params 키워드 다음에는 매개 변수를 추가할 수 없으며 params 키워드 하나만 메서드 선언에 사용할 수 있습니다.

예제
// cs_params.cs using System;
public class MyClass
{
public static void UseParams(params int[] list)
{
for ( int i = 0 ; i

< list.Length ; i++ )
Console.WriteLine(list

);
      Console.WriteLine();
  }

  public static void UseParams2(params object[] list)
  {
for ( int i = 0 ; i

< list.Length ; i++ )
Console.WriteLine(list

);
      Console.WriteLine();
  }

  public static void Main()
  {
UseParams(1, 2, 3);
UseParams2(1, ‘a’, “test”);

int[] myarray = new int[3] {10,11,12};
      UseParams(myarray);
  }
}

출력
1 2 3 1 a test 10 11 12

Author Details
Kubernetes, DevSecOps, AWS, 클라우드 보안, 클라우드 비용관리, SaaS 의 활용과 내재화 등 소프트웨어 개발 전반에 도움이 필요하다면 도움을 요청하세요. 지인이라면 가볍게 도와드리겠습니다. 전문적인 도움이 필요하다면 저의 현업에 방해가 되지 않는 선에서 협의가능합니다.
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments