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. 20160128_关于SVN提交不了并且还提示升级的解决方法

    因为是在项目中新增了文件夹,可能是直接在文件夹里放入文件,导致svn没有读取到文件信息,所以提交不了. 搞了一晚上,蛋疼死,解决方法:把新增的文件夹复制出来,删掉里面的 .svn文件夹.然后删掉项目里 ...

  2. 图解如何 将Excel里的数据导入到sql server数据库中

    项目中,经常会碰到如何将Excel里的数据导入到sql server中的问题. 下面,图解如何实现导入Excel中的数据到sql server 2008 R2: Excel截图如下: 查询pub数据库 ...

  3. 【裸最小生成树】 模板 poj 1258

    #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #def ...

  4. gameUnity 0.15 beta 网络游戏框架

    增加了 换人 和换衣服 ,加入了动画事件.beta版本 0.15测试版本 目前就到此,正式版本 会在7月底  和 0.2版本一起推出.届时,换装系统 将不仅仅是 换装备,而是有 换装后 打怪的体验,更 ...

  5. linux进程、管道和重定向

    1.shell先后使用fork和exec系统调用来执行一个外部命令. 2.在linux系统中,有三个文件会被内核自动打开,分别是stdin.stdout.stderr. 3.进程的属性相关命令: 查看 ...

  6. java 对象的组合,一个类组合到另一个类中(例如手机卡装到手机上)

    Example4_9.java public class Example4_9 { public static void main(String args[]) { SIM simOne = new ...

  7. Tomcat 虚拟目录映射

    最近老是被一个旧Ant工程所困扰,代码版本都改好了测试也通过了,就是打不了war包,一看build.xml 我的天 各种逆天啊....头大.于是乎想起了最基础的tomcat虚拟目录虽是一个很基础的点, ...

  8. as3 公式

    AS3缓动公式:sprite.x += (targetX - sprite.x) * easing;//easing为缓动系数变量sprite.y += (targetY - sprite.y) * ...

  9. android touchEvent事件学习

    学习网址:http://www.apkbus.com/forum.php?mod=viewthread&tid=44296 1:Android Touch事件传递机制解析 android系统中 ...

  10. Hibernate一级缓存和二级缓存深度比较

    1.什么是缓存 缓存是介于应用程序和物理数据源之间,其作用是为了降低应用程序对物理数据源访问的频次,从而提高了应用的运行性能.缓存内的数据是对物理数据源中的数据的复制,应用程序在运行时从缓存读写数据, ...