C#中Tuple的使用
2024-08-30 07:03:28
鉴于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个值。
最新文章
- 实现多表关联来方便你的SELECT查询功能
- 根据xsd生成C#类
- Windows消息传递机制详解
- 关于obj和基本类通过函数参数传进去执行是否改变原来的值
- 关闭“JDK自动更新”提示
- mysql SQL SERVER 的算法
- 剑指Offer29 连续子数组最大和
- 【BZOJ 1491】 [NOI2007]社交网络
- wamp下开启SSL,解决APACHE启动问题
- Java多线程初学者指南(10):使用Synchronized关键字同步类方法
- linux ulimit的使用,如何产生core文件,调试段错误
- Pascal&;#39;s Triangle II
- Spring定时器实现(一)
- Ext.grid.EditorGridPanel点击单元格添加菜单栏
- 第一数学归纳法 vs 第二数学归纳法 vs 良序定理
- [线段树]P1047 校门外的树
- python 全栈开发,Day11(函数名应用,闭包,装饰器初识,带参数以及带返回值的装饰器)
- SPOJ - TSUM 母函数+FFT+容斥
- py-day2-2 python 元祖
- 动态规划(dp)专题