logo

Paramètres C#

En C#, paramètres est un mot-clé utilisé pour spécifier un paramètre qui prend un nombre variable d'arguments. C'est utile lorsque nous ne connaissons pas le nombre d'arguments préalables. Un seul mot-clé params est autorisé et aucun paramètre supplémentaire n'est autorisé après le mot-clé params dans une déclaration de fonction.

Exemple de paramètres 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;>

Exemple de paramètres C# 2

Dans cet exemple, nous utilisons des paramètres de type d'objet qui permettent de saisir n'importe quel nombre d'entrées de n'importe quel 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;>