1. 静态using(static using)

静态using声明允许不使用类名直接调用静态方法。

The static using declaration allows invoking static methods without the class
name.

In C# 5

using System;

Console.WriteLine("Hello, World!");

In C# 6

using static System.Console;

WriteLine("Hello, World");

2. 表达式方法(Expression-Bodied Methods)

使用表达式方法,只有一条语句的方法可以使用lambda语法写。

With expression-bodied methods, a method that includes just one statement can
be written with the lambda syntax.

In C# 5

public bool IsSquare(Rectangle rect)
{
return rect.Height == rect.Width;
}

In C# 6

public bool IsSquare(Rectangle rect) => rect.Height == rect.Width;

3. 表达式属性(Expression-Bodied Properties)

跟表达式方法类似,只有一个get访问器的单行属性可以使用lambda语法写。

Similar to expression-bodied methods, one-line properties with only a get accessor
can be written with the lambda syntax

In C# 5

public string FullName
{
get
{
return FirstName +"" + LastName;
}
}

In C# 6

public string FullName => FirstName +"" + LastName;

4. 自动属性初始化器(Auto-Implemented Property Intializers)

自动属性可以使用属性初始化器初始化。

Auto-implemented properties can be initialized with a property initializer.

In C# 5

public class Person
{
public Person()
{
Age = 24;
}
public int Age {get; set;}
}

In C# 6

public class Person
{
public int Age {get; set;} = 42;
}

5. 只读自动属性(Read-Only Auto Properties)

C# 5需要完整的属性语法实现只读属性,C# 6可以使用自动属性实现。

To implement read-only properties, C# 5 requires the full property syntax. With
C# 6, you can do this using auto-implemented properties.

In C# 5

private readonly int _bookId;
public BookId
{
get
{
return _bookId;
}
}

In C# 6

public BookId {get;}

6. nameof操作符(nameof Operator)

字段、属性、方法和类型的name可以通过nameof访问。使用nameof,可以方便的重构name变化。

With the new nameof operator, names of fields, properties, methods, or types can
be accessed. With this, name changes are not missed with refactoring.

In C# 5

public void Method(object o)
{
if (o == null) throw new ArgumentNullException("o");

In C# 6

public void Method(object o)
{
if (o == null) throw new ArgumentNullException(nameof(o));

7. Null传递操作符(Null Propagation Operator)

Null传递操作符简化了空值检查。

The null propagation operator simplifies null checks.

In C# 5

int? age = p == null ? null : p.Age;
var handler = Event;
if (handler != null)
{
handler(source, e);
}

In C# 6

int? age = p?.Age;
handler?.Invoke(source, e);

8. 字符串插值(String Interpolation)

字符串差值移除了对string.Format的调用,使用表达式占位符取代数字格式占位符。

The string interpolation removes calls to string.Format. Instead of using
numbered format placeholders in the string, the placeholders can include
expressions.

In C# 5

public override ToString()
{
return string.Format("{0}, {1}", Title, Publisher);
}

In C# 6

public override ToString() => $"{Title} {Publisher}";

9. 字典初始化器(Dictionary Initializers)

字典可以使用类似集合的字典初始化器初始化。

Dictionaries can now be initialized with a dictionary initializer—similar to the
collection initializer.

In C# 5

var dict = new Dictionary<int, string>();
dict.Add(3,"three");
dict.Add(7,"seven");

In C# 6

var dict = new Dictionary<int, string>()
{
[3] ="three",
[7] ="seven"
};

10. 异常过滤器(Exception Filters)

异常过滤器允许你在捕获异常前进行过滤。

Exception filters allow you to filter exceptions before catching them.

In C# 5

try
{
//etc.
} catch (MyException ex)
{
if (ex.ErrorCode != 405) throw;
// etc.
}

In C# 6

try
{
//etc.
} catch (MyException ex) when (ex.ErrorCode == 405)
{
// etc.
}

11. 在Catch使用Await(Await in Catch)

await可以在catch块中直接使用,C# 5中需要变通使用。

await can now be used in the catch clause. C# 5 required a workaround.

In C# 5

bool hasError = false;
string errorMessage = null;
try
{
//etc.
} catch (MyException ex)
{
hasError = true;
errorMessage = ex.Message;
}
if (hasError)
{
await new MessageDialog().ShowAsync(errorMessage);
}

In C# 6

try
{
//etc.
} catch (MyException ex)
{
await new MessageDialog().ShowAsync(ex.Message);
}

C# 6.0 11个新特性的更多相关文章

  1. 【C++11】新特性——auto的使用

    [C++11]新特性——auto的使用 C++11中引入的auto主要有两种用途:自动类型推断和返回值占位.auto在C++98中的标识临时变量的语义,由于使用极少且多余,在C++11中已被删除.前后 ...

  2. 【C++11】新特性——Lambda函数

    本篇文章由:http://www.sollyu.com/c11-new-lambda-function/ 文章列表 本文章为系列文章 [C++11]新特性--auto的使用 http://www.so ...

  3. 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.----------- ...

  4. 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. ---------- ...

  5. C++反射机制:可变参数模板实现C++反射(使用C++11的新特性--可变模版参数,只根据类的名字(字符串)创建类的实例。在Nebula高性能网络框架中大量应用)

    1. 概要   本文描述一个通过C++可变参数模板实现C++反射机制的方法.该方法非常实用,在Nebula高性能网络框架中大量应用,实现了非常强大的动态加载动态创建功能.Nebula框架在码云的仓库地 ...

  6. Qt5 中对 C++11 一些新特性的封装

    在 Qt5 中,提供更多 C++11 的特性支持,接下来我们将进行详细的说明. slots (槽) 的 Lambda 表达式 Lambda表达式 是 C++11 中的一个新语法,允许定义匿名函数.匿名 ...

  7. 【Qt开发】Qt5 中对 C++11 一些新特性的封装

    C++11 是现在的 C++ 标准的名称,C++11 为 C++ 语言带来很多新特性. 而 Qt 4.8 是 Qt 首个在其 API 中开始使用一些新的 C++11 特性的版本,我之前写过一篇博文:C ...

  8. C# 11 的新特性和改进前瞻

    前言 .NET 7 的开发还剩下一个多月就要进入 RC,C# 11 的新特性和改进也即将敲定.在这个时间点上,不少新特性都已经实现完毕并合并入主分支 C# 11 包含的新特性和改进非常多,类型系统相比 ...

  9. 有史来最大改变 Android 5.0十大新特性

    有史来最大改变 Android 5.0十大新特性 2014.10.16 14:51:31 来源:腾讯数码作者:腾讯数码 ( 0 条评论 )   距离Android系统上一次重大更新不到一年的时间,谷歌 ...

随机推荐

  1. 安装sysbench遇到找不到库文件的问题

    export LD_LIBRARY_PATH=/usr/mysql/lib./configure --prefix=/usr/local/sysbench --with-mysql-includes= ...

  2. select用法

    每一次操作select的时候,总是要出来翻一下资料,不如自己总结一下,以后就翻这里了. 比如<select class="selector"></select&g ...

  3. Response.Flush()

    Response.Buffer=True就是在缓存网页 访问网站一般是程序直接输出网页结果,或从缓存中读取网页结果2种方式.两种方式在速度上是有差异的 设置 Response.Buffer = Tru ...

  4. vs2010在进行数据架构比较时报'text lines should not be null'错误

    通过VS2010进行服务器数据库和本地数据库比较架构(都是sql server 2008 R2)时,弹出“text lines should be not null”错误,如下图: 解决方法:在Vis ...

  5. ng-option小解

    ng-option 有数组,对象两种情况,但目前只接触过数组 数组: label for value in array 分为一般数组和对象数组 一般数组: <select ng-model=&q ...

  6. Dockerfile编写语法

    docker镜像本质上就是一个个基础镜像的堆叠,为了做出我们想要的镜像,我们需要考虑最终镜像所需的所有基础环境,然后一层层堆叠.也就是不断以基础镜像搭建上层镜像. 先看例子: # Version: # ...

  7. java数据结构之二叉树的实现

    java二叉树的简单实现,可以简单实现深度为n的二叉树的建立,二叉树的前序遍历,中序遍历,后序遍历输出. /** *数据结构之树的实现 *2016/4/29 * **/ package cn.Link ...

  8. 最短路径算法——Dijkstra算法

    在路由选择算法中都要用到求最短路径算法.最出名的求最短路径算法有两个,即Bellman-Ford算法和Dijkstra算法.这两种算法的思路不同,但得出的结果是相同的. 下面只介绍Dijkstra算法 ...

  9. JS-DOM元素灵活查找

    用className选择元素 封装成函数 <title>无标题文档</title> <script> /* window.onload=function () { ...

  10. cocos2d3.8.1 使用prebuild提升发布android速度

    1.生成cocos prebuild库 cocos gen-libs -m debug或 cocos gen-libs -m release 2.使用命令创建test项目 cocos new test ...