Csharp/C Sharp/Language Basics/params

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Check the array length for params parameters

<source lang="csharp"> using System; public class Employee {

   public Employee(params string[] _name) {
       switch (_name.Length) {
           case 1:
               name = _name[0];
               break;
           case 2:
               name = _name[0] + " " + _name[1];
               break;
           default:
               break;
       }
   }
   public void GetName() {
       Console.WriteLine(name);
   }
   private string name = "";

} public class Employeenel {

   public static void Main() {
       Employee bob = new Employee("Bob", "Wilson");
       bob.GetName();
   }

}

</source>


Normal parameter and params parameters

<source lang="csharp"> using System; public class Starter {

   public static void Main() {
       Names("F", "F", "B", "A");
       Names("F", 1234, 5678, 9876, 4561);
       Names("F", "C", "D");
   }
   public static void Names(string company,params string[] employees) {
       Console.WriteLine("{0} employees: ",company);
       foreach (string employee in employees) {
           Console.WriteLine(" {0}", employee);
       }
   }
   public static void Names(string company,params int[] emplid) {
       Console.WriteLine("{0} employees: ",company);
       foreach (int employee in emplid) {
           Console.WriteLine(" {0}", employee);
       }
   }
   public static void Names(string company,string empl1, string empl2) {
       Console.WriteLine("{0} employees: ",company);
       Console.WriteLine("  {0}", empl1);
       Console.WriteLine("  {0}", empl2);
   }

}

</source>


Params Array

<source lang="csharp"> using System; using System.Collections.Generic; using System.Text; class Util {

   public static int Sum(params int[] paramList) {
       if (paramList == null) {
           throw new ArgumentException("Util.Sum: null parameter list");
       }
       if (paramList.Length == 0) {
           throw new ArgumentException("Util.Sum: empty parameter list");
       }
       int sumTotal = 0;
       foreach (int i in paramList) {
           sumTotal += i;
       }
       return sumTotal;
   }

} class Program {

   static void Entrance() {
       Console.WriteLine(Util.Sum(10, 9, 8, 7, 6, 5, 4, 3, 2, 1));
   }
   static void Main() {
       try {
           Entrance();
       } catch (Exception ex) {
           Console.WriteLine("Exception: {0}", ex.Message);
       }
   }

}

</source>


Use params to mark parameter

<source lang="csharp"> using System; class ParamsSample {

   static void PrintStrings(params String[] StringArray) {
       for (int i = 0; i < StringArray.Length; i++)
           Console.Write("{0} ", StringArray[i]);
       Console.WriteLine();
   }
   static void Main(String[] args) {
       String names = "K";
       PrintStrings("A");
       PrintStrings(names, "R", "S");
       PrintStrings("R", "E", "C", "S");
   }

}

</source>


use the params feature to write functions which accept a variable number of arguments

<source lang="csharp"> using System; public class MainClass {

   public static void Main(string[] args) {
       int nSum;
       Console.WriteLine("{0}", SumArgs(out nSum, 1, 2, 3));
       int[] nArray = { 4, 5, 6 };
       Console.WriteLine("{0}", SumArgs(out nSum, nArray));
   }
   public static int SumArgs(out int nSum, params int[] list) {
       nSum = 0;
       foreach (int n in list) {
           nSum += n;
       }
       return nSum;
   }

}

</source>