C# 6.0 預覽 (preview in Roslyn)

微軟在今年4月的 //BUILD 大會中宣佈了 Roslyn 這個計畫,其中對 .NET 開發者最大的意義,不僅在於開放了 .NET Compiler 的原始碼,同時還能參與下一代 .NET 語言的改進;也就是說,也許各位的建議及貢獻能成為下一版本的 .NET 的一部份!

Rosly 網址: https://roslyn.codeplex.com/

隨著計劃的進行,下一版本的 .NET 規格也進入倒數,以下影片簡單展示在 C# 6.0 中的 Primary Constructor, Getter-only auto properties, Method expressions 及 Property expressions:

C#6.0 預覽: Primary Constructor, Getter-only auto properties, Method expressions and Property expressions.

各位可以在 Roslyn 中直接觀察下一代 C#/VB 的實作狀況 (as of 2014/09/11): https://roslyn.codeplex.com/wikipage?title=Language%20Feature%20Status&referringTitle=Home 

Feature Example C# VB
Primary constructors class Point (int x, int y) { … } Done Maybe
Auto-property initializers public int X { get; set; } = x; Done Exists
Getter-only auto-properties public int Y { get; } = y; Done Done
Using static members using System.Console; … Write(4); Done Exists
Dictionary initializer new JObject { ["x"] = 3, ["y"] = 7 } Done Planned
Indexed member initializer new JObject { $x = 3, $y = 7 } Withdrawn Planned
Indexed member access c. $name = c. $first + " " + c. $last; Withdrawn Exists
Declaration expressions int.TryParse(s, out var x); Done Maybe
Await in catch/finally try … catch { await … } finally { await … } Done Planned
Exception filters catch(E e) if (e.Count > 5) { … } Done Exists
Typecase Select Case o : Case s As String : … No Maybe
Guarded cases Select Case i : Case Is > 0 When i Mod 2 = 0 No Maybe
Partial modules Partial Module M1 N/A Done
Partial interfaces Partial Interface I1 Exists Done
Multiline string literals "Hello <newline> World" Exists Done
Year-first date literals Dim d = #2014-04-03# N/A Done
Binary literals 0b00000100 Planned Planned
Digit separators 0xEF _ FF _ 00 _ A0 Planned Planned
Line continuation comments Dim addrs = From c in Customers ' comment N/A Done
TypeOf IsNot If TypeOf x IsNot Customer Then … N/A Done
Expression-bodied members public double Dist => Sqrt(X * X + Y * Y); Planned No
Event initializers new Customer { Notify += MyHandler }; Planned Planned
Null propagation customer ? .Orders ? [5] ? .$price Done Planned
Semicolon operator (var x = Foo() ; Write(x) ; x * x) Maybe Maybe
Private protected private protected string GetId() { … } Withdrawn Withdrawn
Params IEnumerable int Avg(params IEnumerable<int> numbers) { … } Planned Planned
Constructor Inference new Tuple(3, "three", true); Maybe Maybe
String interpolation " \{ p.First } \{ p.Last } is \{ p.Age } years old." Maybe Maybe
TryCast for nullable Dim x = TryCast(u, Integer? ) Exists Planned
Delegate combination with + d1 += d2 Exists Planned
Implicit implementation Class C : Implicitly Implements I Exists Planned
nameof operator string s = nameof( Console.Write ) ; Exists Planned
Strict modules Strict Module M Exists Planned
Faster CInt Dim x = CInt(Math.Truncate(d)) | Exists Planned
#pragma #Disable Warning BC40008 Exists Planned
Checked and Unchecked blocks Checked : x += 1 : End Checked Exists Maybe
Field targets on autoprops <Field: Serializable> Property p As Integer Planned Planned
If(b,e1,e2) uses type context Dim x As Integer? = If(b,1,Nothing) N/A Maybe