[C#] 명명적 인수와 선택적 인수 (named and optional parameter)

less than 1 minute read

가독성을 높이기 위해 인자값으로 함수의 파라미터명과 같이 전달할 수 있다.

Reference

Concept

  • 파라미터명과 같이 함수에 기입하여 가독성을 높일 수 있고, 지정하지 않으면 디폴트값으로 설정하게할 수 있다.
  • C# 4부터 지원함
public void Example(int required, string StrVal = "default", int IntVal = 0)
{
    // ...
}

public void Test()
{
    // This gives compiler error
    // Example(1, 10);

    // This works
    Example(1, IntVal:10);
}

Comments