C# 7.0 新特性1: 基于Tuple的“多”返回值方法
本文基于Roslyn项目中的Issue:#347 展开讨论.
1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法
回顾
首先,提出一个问题,C#中,如何使一个方法可返回"多个"返回值?
我们先来回顾一下C#6.0 及更早版本的做法。
在C#中,通常我们有以下4种方式使一个方法返回多条数据。
- 使用 KeyValue 组合
static void Main(string[] args)
{
int int1 = ;
int int2 = ;
var result = Add_Multiply(int1, int2);
Console.WriteLine(result.Key);
Console.WriteLine(result.Value);
} private static KeyValuePair<int, int> Add_Multiply(int int1, int int2)
{
var KeyValuePair = new KeyValuePair<int, int>(int1 + int2, int1 * int2);
return KeyValuePair;
}
- 使用 ref/out 参数
- Ref
static void Main(string[] args)
{
int int1 = ;
int int2 = ;
int add = ;
int multiply = ;
Add_Multiply(int1, int2, ref add, ref multiply);
Console.WriteLine(add);
Console.WriteLine(multiply);
} private static void Add_Multiply(int int1, int int2, ref int add, ref int multiply)
{
add = int1 + int2;
multiply = int1 * int2;
}- Out
static void Main(string[] args)
{
int int1 = ;
int int2 = ;
int add = ;
int multiply = ;
Add_Multiply(int1, int2, out add, out multiply);
Console.WriteLine(add);
Console.WriteLine(multiply);
} private static void Add_Multiply(int int1, int int2, out int add, out int multiply)
{
add = int1 + int2;
multiply = int1 * int2;
}
- 使用 struct 或者 class
- struct
struct Result
{
public int add;
public int multiply;
}
static void Main(string[] args)
{
int int1 = ;
int int2 = ;
var result = Add_Multiply(int1, int2);
Console.WriteLine(result.add);
Console.WriteLine(result.multiply);
} private static Result Add_Multiply(int int1, int int2)
{
var result = new Result
{
add = int1 + int2,
multiply = int1 * int2
};
return result;
}- class
class Result
{
public int add;
public int multiply;
}
static void Main(string[] args)
{
int int1 = ;
int int2 = ;
var result = Add_Multiply(int1, int2);
Console.WriteLine(result.add);
Console.WriteLine(result.multiply);
} private static Result Add_Multiply(int int1, int int2)
{
var result = new Result
{
add = int1 + int2,
multiply = int1 * int2
};
return result;
}- dynamic
static void Main(string[] args)
{
int int1 = ;
int int2 = ;
var result = Add_Multiply(int1, int2);
Console.WriteLine(result.add);
Console.WriteLine(result.multiply);
} private static dynamic Add_Multiply(int int1, int int2)
{
var result = new
{
add = int1 + int2,
multiply = int1 * int2
};
return result;
}
- 使用 Tuple
static void Main(string[] args)
{
int int1 = ;
int int2 = ;
var result = Add_Multiply(int1, int2);
Console.WriteLine(result.Item1);
Console.WriteLine(result.Item2);
} private static Tuple<int, int> Add_Multiply(int int1, int int2)
{
var tuple = new Tuple<int, int>(int1 + int2, int1 * int2);
return tuple;
}
Okay, 回顾的废话有些多了。我们来看看C#7.0中的写法
新特性(C#7.0)
老规矩,先上代码
static void Main(string[] args)
{
int int1 = ;
int int2 = ;
var result = Add_Multiply(int1, int2);
Console.WriteLine($"Add: {result.add}, Multiply: {result.multiply}");
//(var add, var multiply) = Add_Multiply(int1, int2);
//Console.WriteLine($"Add: {add}, Multiply: {multiply}");
}
public (int add, int multiply) Add_Multiply(int int1, int int2)
=> (int1 + int2, int1 * int2);
怎么样?比起6.0及以前的C#,有没有一种非常清爽的感觉。
其实只是基于Tuple 做了语法简化的语法糖罢了,只是给人一种多个返回值的错觉。
总结
这个特性虽然不是多么振奋人心的变化,但是解决了之前很多码农的一些痒点。
1. 看看KeyValue对的方式,本来很简单的一个操作,写出来的代码会显得非常的笨拙,取值的时候又根据Key获取。而且,最重要的是,如果不在运行时,外面调用的代码是不知道有那些Key的。
2. 再说Ref/Out,这种方式应该是传统意义上最流行的写法了。甚至C#7.0的该特性,也无法取缔ref在一定情景下的性质。但至少在ref用于返回值这种情况下,代码体现出的风格明显是和实际逻辑不符合的,明明是返回值,却要以参数的形式进出,非常不合理。
3. struct和class的方式就不多说了,如果你针对的是一个实体,还能讲得通,但如果本身目的是返回多个相关性不大的数据,专门为方法间传递而加一个本没有用处的Model类或结构,只能说是当时解决方案下的无奈。dynamic虽然从表现形式上没有这种问题,但是存在更坑的问题是,除非在运行时,否则外部调用代码根本不知道方法里传出来什么。
4. 说到传统的Tuple,其实是和该特性最接近的了,但是看看调用时的*.Item1,*.Item2 。。天知道都是何物。即使在实现方法里,也让人面对这种只见类型不见实际意义的值表示一头雾水。
本文链接:http://www.cnblogs.com/ylvict/p/5573094.html (转载请注明)
目前(2016年6月)C#7.0还未正式发布,大家如果想体验部分特性,可以去下载VS15预览版,最终发布的语法可能和本文中提及的有所不同,最新动态请大家关注Roslyn项目。
C# 7.0 新特性1: 基于Tuple的“多”返回值方法的更多相关文章
- C# 9.0新特性详解系列之二:扩展方法GetEnumerator支持foreach循环
1.介绍 我们知道,我们要使一个类型支持foreach循环,就需要这个类型满足下面条件之一: 该类型实例如果实现了下列接口中的其中之一: System.Collections.IEnumerable ...
- C# 7.0 新特性2: 本地方法
本文参考Roslyn项目中的Issue:#259. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# 7.0 新特性3: 模式匹配 ...
- C# 7.0 新特性3: 模式匹配
本文参考Roslyn项目Issue:#206,及Docs:#patterns. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# ...
- C# 7.0 新特性4: 返回引用
本文参考Roslyn项目中的Issue:#118. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# 7.0 新特性3: 模式匹配 ...
- 浅谈Tuple之C#4.0新特性那些事儿你还记得多少?
来源:微信公众号CodeL 今天给大家分享的内容基于前几天收到的一条留言信息,留言内容是这样的: 看了这位网友的留言相信有不少刚接触开发的童鞋们也会有同样的困惑,除了用新建类作为桥梁之外还有什么好的办 ...
- C++ 2.0新特性
C++ standard之演化 C++ 98(1.0) C++ 03(TR1, technical Report 1) // 一个实验性的版本 C++ 11(2.0) C++ 14 此次记录涵盖了C+ ...
- atitit.Servlet2.5 Servlet 3.0 新特性 jsp2.0 jsp2.1 jsp2.2新特性
atitit.Servlet2.5 Servlet 3.0 新特性 jsp2.0 jsp2.1 jsp2.2新特性 1.1. Servlet和JSP规范版本对应关系:1 1.2. Servlet2 ...
- c# 6.0新特性(二)
写在前面 上篇文章介绍了c#6.0的using static,Auto Property Initializers,Index Initializers新的特性,这篇文章将把剩下的几个学习一下. 原文 ...
- Hadoop3.0新特性介绍,比Spark快10倍的Hadoop3.0新特性
Hadoop3.0新特性介绍,比Spark快10倍的Hadoop3.0新特性 Apache hadoop 项目组最新消息,hadoop3.x以后将会调整方案架构,将Mapreduce 基于内存+io+ ...
随机推荐
- knockoutjs with绑定导致unobtrusive validation失效的问题
如果最初的时候with绑定的对象是空的,那么with绑定内部的unobtrusive validation规则在提交的时候无法生效,无法进行验证. 解决办法: 在提交的时候(或者with绑定的对象非空 ...
- Chrome浏览器二维码生成插件
猛击就可以使用啦->>>猛击使用 源码如下: 源码打包 源码: jquery-2.1.3.min.js jquery.qrcode.min.js https://gith ...
- IP地址(IPv4)/IPv6地址的正则表达式
原地址:http://pfeishao.blog.163.com/blog/static/18162337020112113130453/ Pv4地址正则表达式:^((25[0-5]|2[0-4]\d ...
- Linux基本操作命令之文件查看cat more less tail head
cat 参考之前博客:Linux基础命令之cat使用方法大全 more 命令 命令:more使用权限:所有使用者使用方式:more [选项] filename说明:类似于cat,不过会一页一页的显示内 ...
- 高性能MySQL笔记 第4章 Schema与数据类型优化
4.1 选择优化的数据类型 通用原则 更小的通常更好 前提是要确保没有低估需要存储的值范围:因为它占用更少的磁盘.内存.CPU缓存,并且处理时需要的CPU周期也更少. 简单就好 简 ...
- shell脚本实现随机筛选
#!/bin/bash name=(val1 val2 val3 val4 ...) a=$() #以时间产生随机数向39取余得到0~38的值
- mysql 错误 1221 Incorrect usage of union and order by
今天有个项目出现了问题 问题原因是union和order by 的问题.关于这个问题的解决方案可以猛击下面的链接. http://blog.csdn.net/gtuu0123/article/deta ...
- cookie实现自动登录
有很多Web程序中第一次登录后,在一定时间内(如2个小时)再次访问同一个Web程序时就无需再次登录,而是直接进入程序的主界面(仅限于本机).实现这个功能关键就是服务端要识别客户的身份.而用Cookie ...
- 移动Windows用户文件夹的方法研究
这种方法可能导致升级Windows失败.请谨慎使用. Windows 8.1 使用有效.其他系统请酌情修改. —————————————————————————— 复制文件内容(带权限等信息):有的说 ...
- 【OpenWRT之旅】如何自定义一个配置文件的设置界面
作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/ 1. 引言 OpenWRT中采用LuCI作为它的Web interface界面框架,采用Lua语言.在本文中将以 ...