C#中Tuple的使用

鉴于MSDN上面的机器翻译实在太烂,还是自己翻译吧,虽然麻烦了点(-_-)。

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

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

一、表示一组数据

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

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

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

        

using System;

public class Example
{
   public static void Main()
   {
      Tuple<string, Nullable<int>>[] scores = 
                    { new Tuple<string, Nullable<int>>("Jack", 78),
                      new Tuple<string, Nullable<int>>("Abbey", 92), 
                      new Tuple<string, Nullable<int>>("Dave", 88),
                      new Tuple<string, Nullable<int>>("Sam", 91), 
                      new Tuple<string, Nullable<int>>("Ed", null),
                      new Tuple<string, Nullable<int>>("Penelope", 82),
                      new Tuple<string, Nullable<int>>("Linda", 99),
                      new Tuple<string, Nullable<int>>("Judith", 84) };
      int number;
      double mean = ComputeMean(scores, out number);
      Console.WriteLine("Average test score: {0:N2} (n={1})", mean, number);
   }

   private static double ComputeMean(Tuple<string, Nullable<int>>[] scores, out int n) 
   {
      n = 0;      
      int sum = 0;
      foreach (var score in scores)
      {
         if (score.Item2.HasValue)
         { 
            n += 1;
            sum += score.Item2.Value;
         }
      }     
      if (n > 0)
         return sum / (double) n;
      else
         return 0;
   }
}
// The example displays the following output:
//       Average test score: 88 (n=7)

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

贴一段我的代码

       

        public Tuple<int, string> ManEntryPN(DateTime recTime, double netLossRate, double electricityOnline, double electricitySell)
        {
            //检验查询
            Tuple<int, string> tuple = null;
            string testProc = "queryManagePageData";
            SqlParameter[] testParas = new SqlParameter[] {
                new SqlParameter("@recTime",recTime),
                new SqlParameter("@netLossRate",netLossRate),
                new SqlParameter("@electricityOnline",electricityOnline),
                new SqlParameter("@electricitySell",electricitySell),
                new SqlParameter("@indexName","TestManEntryPN")
            };
            DataTable dt = new DataTable();
            dt = sqlhelper.ExecuteQuery(testProc, testParas, CommandType.StoredProcedure);
            if (dt.Rows.Count > 0)
            {
                //如果该日期数据已经录入
                return tuple = new Tuple<int, string>(1, recTime + "数据已经录入");
            }

            //数据录入
            string insertProc = "queryManagePageData";
            SqlParameter[] insertParas = new SqlParameter[] {
                new SqlParameter("@recTime",recTime),
                new SqlParameter("@netLossRate",netLossRate),
                new SqlParameter("@electricityOnline",electricityOnline),
                new SqlParameter("@electricitySell",electricitySell),
                new SqlParameter("@indexName","ManEntryPN")
            };
            int res = sqlhelper.ExecuteNonQuery(insertProc, insertParas, CommandType.StoredProcedure);
            if (res > 0)
            {
                //如果录入成功
                return tuple = new Tuple<int, string>(0, "Sucess");
            }
            return tuple = new Tuple<int, string>(1, "插入失败");
        }


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

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

原文地址:https://www.cnblogs.com/haxianhe/p/9271144.html