logo

C#-parametre

I C#, params er et nøgleord, som bruges til at specificere en parameter, der tager variabelt antal argumenter. Det er nyttigt, når vi ikke kender antallet af argumenter før. Kun ét params nøgleord er tilladt, og ingen yderligere parameter er tilladt efter params nøgleord i en funktionserklæring.

C# Params Eksempel 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# Params Eksempel 2

I dette eksempel bruger vi objekttypeparametre, der tillader indtastning af et hvilket som helst antal input af enhver type.

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