param数组参数

来源:http://mcdelfino.blog.51cto.com/2058744/661211

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语句。

语句的执行效果如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值