1. 老版本代码

 class Program
{
static void Main(string[] args)
{
var fullName = GetFullName(); Console.WriteLine(fullName.Item1);// Item1,2,3不能忍,,,
Console.WriteLine(fullName.Item2);
Console.WriteLine(fullName.Item3);
}
static Tuple<string, string, string> GetFullName() => new Tuple<string, string, string>("first name", "blackheart", "last name");
}

在有些场景下,我们需要一个方法返回一个以上的返回值,微软在.NET 4中引入了Tuple这个泛型类,可以允许我们返回多个参数,每个参数按照顺序被命名为 Item1;Item2,Item3 ,算是部分的解决了我们的问题,但是对于强迫症程序员来说,Item1,2,3的命名简直是不能忍的,,,so,在C#7中,引入了一个新的泛型类型ValueTuple<T>来解决这个问题,这个类型位于一个单独的dll(System.ValueTuple)中,可以通过nuget来引入到你当前的项目中(https://www.nuget.org/packages/System.ValueTuple/)。

2. ValueTuple

不废话,直接看代码:

 class Program
{
static void Main(string[] args)
{
var fullName = GetFullName(); Console.WriteLine(fullName.First); // 终于可以不是Item1,2,3了,,,
Console.WriteLine(fullName.Middle);
Console.WriteLine(fullName.Last);
} static (string First, string Middle, string Last) GetFullName() => ("first name", "blackheart", "last name");
}

看出来差别了吗?我们终于可以用更直观的名字来替换掉该死的"Item1,2,3"了,看起来很棒吧。但是貌似我们并没有用到上面我提到的System.ValueTuple,我们翻开编译后的程序集看看:

 internal class Program
{
private static void Main(string[] args)
{
ValueTuple<string, string, string> fullName = Program.GetFullName();
Console.WriteLine(fullName.Item1); // 原来你还是Item1,2,3,,,FUCK!!!
Console.WriteLine(fullName.Item2);
Console.WriteLine(fullName.Item3);
} [TupleElementNames(new string[]
{
"First",
"Middle",
"Last"
})]
private static ValueTuple<string, string, string> GetFullName()
{
return new ValueTuple<string, string, string>("first name", "blackheart", "last name");
}
}

不看不知道,一看吓一跳,原来我们的 fullName.First; 编译后居然还是 fullName.Item1 ,真是日了狗了。。。

不同之处在于GetFullName这个方法,编译器把我们简化的语法形式翻译成了 ValueTuple<string, string, string> ,还给加了一个新的Attribute(TupleElementNamesAttribute),然后把我们自定义的非常直观友好的“First”,"Middle","Last"当作元数据给存起来了(如果只是局部使用,则不会添加这样的元数据)。TupleElementNamesAttribute和ValueTuple一样,位于System.ValueTuple的单独dll中。

3. Example

 class Program
{
static void Main(string[] args)
{
var range = (first: , end: );
//也可以这样写,效果是一样的,编译后都是没有了first,end的痕迹,,,first和end只是语法层面的障眼法
//(int first, int last) range = (1, 10);
Console.WriteLine(range.first);
Console.WriteLine(range.end); //可以使用var,这种无显示声明一个变量的方式会编译出多余的代码,慎用,不知是不是还未优化好。
(var begin, var end) = (DateTime.Parse("2017-1-1"), DateTime.Parse("2017-12-31"));
Console.WriteLine(begin);
Console.WriteLine(end); //begin,end可以被覆盖重命名为startDate和endDate,但是会有一个编译警告,提示名字被忽略掉了。
//warning CS8123: The tuple element name 'begin' is ignored because a different name is specified by the target type '(DateTime startDate, DateTime endDate)'
//warning CS8123: The tuple element name 'end' is ignored because a different name is specified by the target type '(DateTime startDate, DateTime endDate)‘
(DateTime startDate, DateTime endDate) timeSpan = (begin: DateTime.Parse("2017-1-1"), end: DateTime.Parse("2017-12-31"));
Console.WriteLine(timeSpan.startDate);
Console.WriteLine(timeSpan.endDate);
}
}

look一下编译后的代码:

 private static void Main(string[] args)
{
ValueTuple<int, int> range = new ValueTuple<int, int>(, );
Console.WriteLine(range.Item1);
Console.WriteLine(range.Item2);
6 ValueTuple<DateTime, DateTime> expr_3C = new ValueTuple<DateTime, DateTime>(DateTime.Parse("2017-1-1"), DateTime.Parse("2017-12-31"));
7 DateTime item = expr_3C.Item1;
8 DateTime item2 = expr_3C.Item2;
9 DateTime begin = item;
10 DateTime end = item2;
Console.WriteLine(begin);
Console.WriteLine(end);
ValueTuple<DateTime, DateTime> timeSpan = new ValueTuple<DateTime, DateTime>(DateTime.Parse("2017-1-1"), DateTime.Parse("2017-12-31"));
Console.WriteLine(timeSpan.Item1);
Console.WriteLine(timeSpan.Item2);
}

注意 (var begin, var end) = (DateTime.Parse("2017-1-1"), DateTime.Parse("2017-12-31")); 这一行的便宜结果,看起来很是糟糕(上述6-10行红色部分),可能还是编译优化不足的问题吧(release编译也是如此)。

4. 总结

新的语法形式确实直观友好了好多,but,本质依然是借助泛型类型来实现的,同时也需要编译器对新语法形式的支持。

了解了本质是什么东西之后,以后在项目中环境允许的话,就放心大胆的使用吧(类型ValueTuple可以出现的地方,(first,last)这种新语法形式均可以)。

参考:

https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/

https://docs.microsoft.com/en-us/dotnet/articles/csharp/tuples

最新文章

  1. win10用户文件夹重命名,启用administrator账户,删除文件夹时提示找不到该项目
  2. [收藏] javascript keycode大全
  3. AC小笔记
  4. sql 列转行 实例
  5. WebBrowser 多线程 DocumentCompleted 和定时器
  6. 01JavaIO详解_File类
  7. 如何更改java应用程序标题栏默认图标
  8. cocos2dx 2.x mac proj 开启模板
  9. SASS -- 基本认识
  10. 为什么会出现ADB rejected shell command
  11. Windows下如何使用BOOST C++库 .
  12. default parameter value for ‘color’ must be a compile-time constant
  13. Nyoj 一笔画问题(图论)
  14. [河南省ACM省赛-第四届] 序号互换 (nyoj 303)
  15. 2016WHD.china世界云计算日&#183;北京站即将召开
  16. 线性代数之行列式的C#研究实现
  17. poj 1755 半平面交+不等式
  18. java基础 第八章课后习题
  19. 基于ASP.NET 4.0开发的微商城系统OdnShop,开源发布
  20. node day1 login

热门文章

  1. MySQL批量导出以某数字或字母开头的表
  2. QT第一天学习
  3. MapReduce深度分析(一)
  4. JSP EL表达式 获得 request的GET/POST方法
  5. abstract、override、new、virtual、sealed使用和示例
  6. MyBatis 模糊查询
  7. 如何做到Zero Downtime重启Go服务?
  8. es6笔记5^_^set、map、iterator
  9. Ionic在windows下的环境配置难题
  10. hessian原理解析一(客户端分析)