C# 6 新特性
微软新一代开发工具Visual Studio 2015正式版已经于北京时间 2015年7月20日 23:30 发布了,而作为.Net开发中的主力开发语言C#,也已经走到了C# 6。
那么作为C#开发语言的最新版本,微软又给它添加或改进了哪些特性呢?
C# 6的新特性是和微软的 .Net 开源编译器Roslyn紧密相连的,在其 GitHub 页面中,也给出了C# 6新特性:New Language Features in C# 6。下面内容一部分为简单的翻译,以及在查看时的一些理解和疑问,由于目前我还没发下载最新版 Win10 和 VS 2015,因此有些地方暂时还没发验证猜想。等待后续更新。
下面先列出C# 6中的新特性:
自动属性初始值表达式
public int X { get; set; } = x;只读自动属性
public int Y { get; } = y;静态导入
using staticusing static System.Console; // ... Write(4);索引器对象初始化
new JObject { ["x"] = 3 }在
catch和finally块中使用awaittry { ... } catch { await ... } finally { await ... }异常筛选器
catch(Exception e) when (e.Count > 5) { ... }函数/属性表达式
public double Dist => Sqrt(X * X + Y * Y);Null条件运算符
customer?.Orders?[5]字符串插值
$"{p.Name} is {p.Age} years old."nameof运算符string s = nameof(Console.Write);其它改进
- #pragma
- 集合初始化支持扩展方法
Add - 重载方法解析改进
