• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#中Tuple的使用

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

鉴于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, "插入失败");
        }


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

如果你提供Tuple<T1, T2, T3> 对象作为方法参数,则你可以给该线程的启动方法传3个值。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap