據扯,C# 6.0在不遠的未來就發佈了,對應的IDE多是VS 2014(.Net Framework 5.0),由於VS 2013已於2013年10月份發佈了,對應的是.Net Franework 4.5.1。html
從Visual Studio的更新規律上來看,微軟2或者3年,更新增長的東西會比較多,因此對於C# 6.0,仍是有一些期待的。函數
下面這張圖列出了C#每次重要更新的時間及增長的新特性,對於瞭解C#這些年的發展歷程,對C#的認識更加全面,是有幫助的。其中圖的最後一行C#6.0是根據一些博客整理的,若有錯誤,隨時改正。this
一、主構造函數(Primary Constructors)spa
主構造函數給類中的變量賦值code
Beforehtm
public class Point { private int x, y; public Point(int x, int y) this.x = x; this.y = y; } }
public class Point(int x, int y) { private int x, y; }
二、自動屬性賦值(Auto Properties) blog
class Point { public int X { get; private set; } public int Y { get; private set; } public Point() { this.X = 100; this.Y = 100; } }
class Point { public int X { get; private set; } = 100; public int Y { get; private set; } = 100; }
using會把引用類的全部靜態方法導入到當前命名空間ip
public double A { get { return Math.Sqrt(Math.Round(5.142)); } }
using System.Math; ... public double A { get { return Sqrt(Round(5.142)); } }
public double Distance { get { return Math.Sqrt((X * X) + (Y * Y)); } }
public double Distance => Math.Sqrt((X * X) + (Y * Y));
初看起來像Lambda表達式,其實和Lambda無關係。get
public Point Move(int dx, int dy) { return new Point(X + dx1, Y + dy1); }
public Point Move(int dx, int dy) => new Point(X + dx, Y + dy);
這個和Property Expressions相似博客
Do(someEnum.ToArray()); ... public void Do(params int[] values) { ... }
Do(someEnum); public void Do(params IEnumerable<Point> points) { ... }
之前params是隻能夠修飾array類型的參數,如今多了一些類型
if (points != null) { var next = points.FirstOrDefault(); if (next != null && next.X != null) return next.X; } return -1;
var bestValue = points?.FirstOrDefault()?.X ?? -1;
這個少了好幾行代碼,贊啊
var x = MyClass.Create(1, "X"); ... public MyClass<T1, T2> Create<T1, T2>(T1 a, T2 b) { return new MyClass<T1, T2>(a, b); }
var x = new MyClass(1, "X");
int x; int.TryParse("123", out x);
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