logo

C# Params

C# nyelven, params egy olyan kulcsszó, amely változó számú argumentumot felvevő paraméter megadására szolgál. Akkor hasznos, ha nem ismerjük az argumentumok számát. Csak egy params kulcsszó engedélyezett, és további paraméterek nem engedélyezettek a params kulcsszó után egy függvénydeklarációban.

C# paraméterek 1. példa

 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# paraméterek 2. példa

Ebben a példában objektumtípus-paramétereket használunk, amelyek lehetővé teszik tetszőleges számú, bármilyen típusú bemenet megadását.

 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;>