C# 6.0可能的新特性
C# 6.0可能的新特性
1、主构造函数(Primary Constructors)
主构造函数给类中的变量赋值
Before

public class Point {
private int x, y; public Point(int x, int y)
this.x = x;
this.y = y;
}
}

After
public class Point(int x, int y) {
private int x, y;
}
2、自动属性赋值(Auto Properties)
Before

class Point
{
public int X { get; private set; }
public int Y { get; private set; } public Point()
{
this.X = 100;
this.Y = 100;
}
}

After
class Point
{
public int X { get; private set; } = 100;
public int Y { get; private set; } = 100;
}
3、using静态类(Static type using statements;)
using会把引用类的所有静态方法导入到当前命名空间
Before
public double A { get { return Math.Sqrt(Math.Round(5.142)); } }
After
using System.Math;
...
public double A { get { return Sqrt(Round(5.142)); } }
4、Property Expressions
Before
public double Distance {
get { return Math.Sqrt((X * X) + (Y * Y)); }
}
After
public double Distance => Math.Sqrt((X * X) + (Y * Y));
初看起来像Lambda表达式,其实和Lambda无关系。
5. Method Expressions
Before
public Point Move(int dx, int dy) {
return new Point(X + dx1, Y + dy1);
}
After
public Point Move(int dx, int dy) => new Point(X + dx, Y + dy);
这个和Property Expressions类似
6、Params for enumerables
Before
Do(someEnum.ToArray());
...
public void Do(params int[] values) { ... }
After
Do(someEnum);
public void Do(params IEnumerable<Point> points) { ... }
以前params是只可以修饰array类型的参数,现在多了一些类型
7、Monadic null checking(null检查运算符)
Before
if (points != null) {
var next = points.FirstOrDefault();
if (next != null && next.X != null) return next.X;
}
return -1;
After
var bestValue = points?.FirstOrDefault()?.X ?? -1;
这个少了好几行代码,赞啊
8、Constructor type parameter inference
Before
var x = MyClass.Create(1, "X");
...
public MyClass<T1, T2> Create<T1, T2>(T1 a, T2 b) {
return new MyClass<T1, T2>(a, b);
}
After
var x = new MyClass(1, "X");
9、 Inline declarations for out params(内联out参数定义)
Before
int x;
int.TryParse("123", out x);
After
int.TryParse("123", out int x);
说实话,这个很常用。
当然,C# 6.0的全部新特性不止这么多,限于篇幅,就整理这些,若想了解更多,请参考下面的链接。
参考资料:
http://roslyn.codeplex.com/wikipage?title=Language%20Feature%20Status&referringTitle=Documentation
http://en.wikipedia.org/wiki/.NET_Framework_version_history
http://damieng.com/blog/2013/12/09/probable-c-6-0-features-illustrated
http://www.kunal-chowdhury.com/2012/07/evolution-of-c-10-50-what-are-new.html
C# 6.0可能的新特性的更多相关文章
- 有史来最大改变 Android 5.0十大新特性
有史来最大改变 Android 5.0十大新特性 2014.10.16 14:51:31 来源:腾讯数码作者:腾讯数码 ( 0 条评论 ) 距离Android系统上一次重大更新不到一年的时间,谷歌 ...
- C# 6.0可能的新特性及C#发展历程
据扯,C# 6.0在不远的将来就发布了,对应的IDE可能是VS 2014(.Net Framework 5.0),因为VS 2013已于2013年10月份发布了,对应的是.Net Franework ...
- Spring Boot 2.0正式发布,新特性解读
作者|翟永超 Spring Boot 2.0 来啦,有哪些新特性?升级吗? 写在前面 北京时间 3 月 1 日,经过漫长的等待之后,Spring Boot 2.0 正式发布.作为 Spring 生态中 ...
- C# 6.0可能的新特性及C#发展历程[转]
C# 6.0可能的新特性及C#发展历程[转] 年10月份发布了,对应的是.Net Franework 4.5.1. 或者3年,更新增加的东西会比较多,所以对于C# 6.0,还是有一些期待的. 下面 ...
- MySQL 8.0.2复制新特性(翻译)
译者:知数堂星耀队 MySQL 8.0.2复制新特性 MySQL 8 正在变得原来越好,而且这也在我们MySQL复制研发团队引起了一阵热潮.我们一直致力于全面提升MySQL复制,通过引入新的和一些有趣 ...
- Atitit.c# .net 3.5 4.0 4.5 5.0 6.0各个版本新特性战略规划总结
Atitit.c# .net 3.5 4.0 各个版本新特性战略规划总结 1. --------------.Net Framework版本同CLR版本的关系1 2. paip.----------- ...
- c# .net 3.5 4.0 4.5 5.0 6.0各个版本新特性战略规划总结【转载】
引用:http://blog.csdn.net/attilax/article/details/42014327 c# .net 3.5 4.0 各个版本新特性战略规划总结 1. ---------- ...
- 转载——C# 6.0可能的新特性及C#发展历程
据扯,C# 6.0在不远的将来就发布了,对应的IDE可能是VS 2014(.Net Framework 5.0),因为VS 2013已于2013年10月份发布了,对应的是.Net Franework ...
- 【HANA系列】SAP HANA 1.0 SPS 11 新特性
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA 1.0 SPS ...
随机推荐
- windows无法安装到这个磁盘怎样解决
装操作系统,出提示:windows无法安装到这个磁盘.这台计算机的硬件可能不支持启动到此盘.你甚至用专业的分区软件都无法解决这个问题,比如说PM等.一般在更换好新的硬盘的时候或者将操作系统装入移动硬盘 ...
- Segment-Based stereo matching
首先代码实现是根据"Segment-Based Stereo Matching Using Belief Propogation and Self-Adapting Dissimilarit ...
- 【LintCode】删除链表中的元素
问题分析: 声明当前指针和上一个指针即可. 问题求解: public class Solution { public ListNode removeElements(ListNode head, in ...
- BZOJ2120 数颜色(带修改莫队)
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...
- hdu5183 hash大法
维护前缀和sum[i]=a[0]-a[1]+a[2]-a[3]+…+(-1)^i*a[i]枚举结尾i,然后在hash表中查询是否存在sum[i]-K的值.如果当前i为奇数,则将sum[i]插入到has ...
- Android中运行的错误:java.lang.UnsatisfiedLinkError: Couldn't load locSDK3: findLibrary returned null.
---恢复内容开始--- 明明已经加入了liblocSDK3.so,但总是无法定位.提示错误java.lang.UnsatisfiedLinkError: Couldn't load locSDK3: ...
- Scala高阶函数示例
object Closure { def function1(n: Int): Int = { val multiplier = (i: Int, m: Int) => i * m multip ...
- WPF控件ComboBox 每个Item的ToolTip引发的异常
介绍 首先介绍下要实现的任务.做一个下拉框,当选择每个项的时候将鼠标发在上面显示该项的ToolTip的内容(Image). 实现 Model: public class SkinInfo : Noti ...
- CF570D:Tree Requests
传送门 DFS重标号+二分 打比赛的时候想了很多方法..DFS序,BFS序,倍增什么的都考虑了一遍,但是几乎要么是可以维护两个区间但是代码复杂度爆炸,要么就是只能维护单一维度的信息. 这道题的具体做法 ...
- 堆优化的Dijkstra
SPFA在求最短路时不是万能的.在稠密图时用堆优化的dijkstra更加高效: typedef pair<int,int> pii; priority_queue<pii, vect ...