logo

C# 매개변수

C#에서는 매개변수 가변 개수의 인수를 취하는 매개변수를 지정하는 데 사용되는 키워드입니다. 이전 인수의 수를 모를 때 유용합니다. params 키워드는 하나만 허용되며 함수 선언에서 params 키워드 뒤에는 추가 매개 변수가 허용되지 않습니다.

C# 매개변수 예제 1

 using System; namespace AccessSpecifiers { class Program { // User defined function public void Show(params int[] val) // Params Paramater { for (int i=0; i<val.length; i++) { console.writeline(val[i]); } main function, execution entry point of the program static void main(string[] args) program(); creating object program.show(2,4,6,8,10,12,14); passing arguments variable length < pre> <p> <strong>Output:</strong> </p> <pre> 2 4 6 8 10 12 14 </pre> <h3>C# Params Example 2</h3> <p>In this example, we are using object type params that allow entering any number of inputs of any type.</p> <pre> using System; namespace AccessSpecifiers { class Program { // User defined function public void Show(params object[] items) // Params Paramater { for (int i = 0; i <items.length; i++) { console.writeline(items[i]); } main function, execution entry point of the program static void main(string[] args) program(); creating object program.show('ramakrishnan ayyer','ramesh',101, 20.50,'peter', 'a'); passing arguments variable length < pre> <p> <strong>Output:</strong> </p> <pre> Ramakrishnan Ayyer Ramesh 101 20.5 Peter A </pre> <br></items.length;></pre></val.length;>

C# 매개변수 예제 2

이 예에서는 모든 유형의 입력을 원하는 만큼 입력할 수 있는 객체 유형 매개변수를 사용하고 있습니다.

 using System; namespace AccessSpecifiers { class Program { // User defined function public void Show(params object[] items) // Params Paramater { for (int i = 0; i <items.length; i++) { console.writeline(items[i]); } main function, execution entry point of the program static void main(string[] args) program(); creating object program.show(\'ramakrishnan ayyer\',\'ramesh\',101, 20.50,\'peter\', \'a\'); passing arguments variable length < pre> <p> <strong>Output:</strong> </p> <pre> Ramakrishnan Ayyer Ramesh 101 20.5 Peter A </pre> <br></items.length;>