param數組參數

param數組允許我們只寫一個方法, 就能接受數量可變的參數. 這種技術就是參數數組,它本質上是用params關鍵字來聲明的一個參數.而且不僅可以聲明params int[] list這樣的數組,還可以聲明object類型的參數組,參數可以是任意類型的~

Util類

  1. #region Using directives 
  2. using System; 
  3. #endregion 
  4.  
  5. namespace ParamsArray 
  6.     class Util 
  7.     { 
  8.         public static int Sum(params int[] paramList) 
  9.         { 
  10.             if (paramList == null
  11.             { 
  12.                 throw new ArgumentException("Util.Sum: null parameter list"); 
  13.             } 
  14.  
  15.             if (paramList.Length == 0) 
  16.             { 
  17.                 throw new ArgumentException("Util.Sum: empty parameter list"); 
  18.             } 
  19.  
  20.             int sumTotal = 0; 
  21.             foreach (int i in paramList) 
  22.             { 
  23.                 sumTotal += i; 
  24.             } 
  25.             return sumTotal; 
  26.         } 
  27.  
  28.         public static void Everyone(params object[] paramobject) 
  29.         { 
  30.             if (paramobject == null
  31.             { 
  32.                 throw new Exception("Util.Everyone: null parameter list"); 
  33.             } 
  34.  
  35.             if (paramobject.Length == 0) 
  36.             { 
  37.                 throw new Exception("Util.Everyone: empty parameter list"); 
  38.             } 
  39.  
  40.             foreach (object i in paramobject) 
  41.             { 
  42.                 Console.WriteLine(i); 
  43.             } 
  44.         } 
  45.     } 

Program類

  1. #region Using directives 
  2.  
  3. using System; 
  4. using System.Collections.Generic; 
  5. using System.Text; 
  6.  
  7. #endregion 
  8.  
  9. namespace ParamsArray 
  10.     class Program 
  11.     { 
  12.         static void Entrance() 
  13.         { 
  14.             Console.WriteLine(Util.Sum(10, 9, 8, 7, 6, 5, 4, 3, 2, 1));      
  15.         } 
  16.  
  17.         static void Entrance01() 
  18.         { 
  19.             Console.WriteLine(Util.Sum());      //長度爲0 
  20.         } 
  21.  
  22.         static void Entrance02() 
  23.         { 
  24.             Console.WriteLine(Util.Sum(null));  //數組爲null 
  25.         } 
  26.  
  27.         static void Entrance03() 
  28.         { 
  29.             Util.Everyone("dkjf", 45, "654", 451.5); 
  30.         } 
  31.  
  32.         static void Main() 
  33.         { 
  34.             try 
  35.             { 
  36.                 Entrance(); 
  37.                 Entrance01(); 
  38.             } 
  39.             catch (Exception ex) 
  40.             { 
  41.                 Console.WriteLine("Exception: {0}", ex.Message); 
  42.             } 
  43.  
  44.             try 
  45.             { 
  46.                 Entrance02(); 
  47.             } 
  48.             catch (Exception ex) 
  49.             { 
  50.                 Console.WriteLine("Exception: {0}", ex.Message); 
  51.             } 
  52.  
  53.             try 
  54.             { 
  55.                 Entrance03(); 
  56.             } 
  57.             catch (System.Exception ex) 
  58.             { 
  59.                 Console.WriteLine("Exception: {0}", ex.Message); 
  60.             } 
  61.         } 
  62.     } 

每個try語句執行後,try語句內部的在Exception之後的部分就不會再執行了,所以要另外再寫一個try語句。

語句的執行效果如下:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章