C#中Tuple的使用


定义:元组是具有 特定数量和序列 的元素 的数据结构  (注意断句哈!)

元组通常有四种使用方式︰

一、表示一组数据

例如,一个元组可以表示一条数据库记录,并且每一个分量对应表示这条记录的每个字段便于对数据集进行访问和操作,例如下面这个例子(数据集市每个学生和他的分数,最后求出所有成绩的学生的平均分数):

二、便于对数据集进行访问和操作

例如下面这个例子(数据集市每个学生和他的分数,最后求出所有成绩的学生的平均分数):

        

[csharp]  view plain  copy
 print ?
  1. using System;  
  2.   
  3. public class Example  
  4. {  
  5.    public static void Main()  
  6.    {  
  7.       Tuple<string, Nullable<int>>[] scores =   
  8.                     { new Tuple<string, Nullable<int>>("Jack", 78),  
  9.                       new Tuple<string, Nullable<int>>("Abbey", 92),   
  10.                       new Tuple<string, Nullable<int>>("Dave", 88),  
  11.                       new Tuple<string, Nullable<int>>("Sam", 91),   
  12.                       new Tuple<string, Nullable<int>>("Ed"null),  
  13.                       new Tuple<string, Nullable<int>>("Penelope", 82),  
  14.                       new Tuple<string, Nullable<int>>("Linda", 99),  
  15.                       new Tuple<string, Nullable<int>>("Judith", 84) };  
  16.       int number;  
  17.       double mean = ComputeMean(scores, out number);  
  18.       Console.WriteLine("Average test score: {0:N2} (n={1})", mean, number);  
  19.    }  
  20.   
  21.    private static double ComputeMean(Tuple<string, Nullable<int>>[] scores, out int n)   
  22.    {  
  23.       n = 0;        
  24.       int sum = 0;  
  25.       foreach (var score in scores)  
  26.       {  
  27.          if (score.Item2.HasValue)  
  28.          {   
  29.             n += 1;  
  30.             sum += score.Item2.Value;  
  31.          }  
  32.       }       
  33.       if (n > 0)  
  34.          return sum / (double) n;  
  35.       else  
  36.          return 0;  
  37.    }  
  38. }  
  39. // The example displays the following output:  
  40. //       Average test score: 88 (n=7)  

三、一个方法有多个返回值无需使用out参数(事实上我就是用的这种方式)

贴一段我的代码

       

[csharp]  view plain  copy
 print ?
  1. public Tuple<intstring> ManEntryPN(DateTime recTime, double netLossRate, double electricityOnline, double electricitySell)  
  2. {  
  3.     //检验查询  
  4.     Tuple<intstring> tuple = null;  
  5.     string testProc = "queryManagePageData";  
  6.     SqlParameter[] testParas = new SqlParameter[] {  
  7.         new SqlParameter("@recTime",recTime),  
  8.         new SqlParameter("@netLossRate",netLossRate),  
  9.         new SqlParameter("@electricityOnline",electricityOnline),  
  10.         new SqlParameter("@electricitySell",electricitySell),  
  11.         new SqlParameter("@indexName","TestManEntryPN")  
  12.     };  
  13.     DataTable dt = new DataTable();  
  14.     dt = sqlhelper.ExecuteQuery(testProc, testParas, CommandType.StoredProcedure);  
  15.     if (dt.Rows.Count > 0)  
  16.     {  
  17.         //如果该日期数据已经录入  
  18.         return tuple = new Tuple<intstring>(1, recTime + "数据已经录入");  
  19.     }  
  20.   
  21.     //数据录入  
  22.     string insertProc = "queryManagePageData";  
  23.     SqlParameter[] insertParas = new SqlParameter[] {  
  24.         new SqlParameter("@recTime",recTime),  
  25.         new SqlParameter("@netLossRate",netLossRate),  
  26.         new SqlParameter("@electricityOnline",electricityOnline),  
  27.         new SqlParameter("@electricitySell",electricitySell),  
  28.         new SqlParameter("@indexName","ManEntryPN")  
  29.     };  
  30.     int res = sqlhelper.ExecuteNonQuery(insertProc, insertParas, CommandType.StoredProcedure);  
  31.     if (res > 0)  
  32.     {  
  33.         //如果录入成功  
  34.         return tuple = new Tuple<intstring>(0, "Sucess");  
  35.     }  
  36.     return tuple = new Tuple<intstring>(1, "插入失败");  
  37. }  


、将多个值传给单个参数的方法

例如,Thread.Start(Object) 方法只有一个参数,即你可以传一个值给该线程的启动方法。 如果你提供Tuple<T1, T2, T3> 对象作为方法参数,则你可以给该线程的启动方法传3个值

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值