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. Win32 SDK Combo Box

    如下图所示,显示了三种不同风格的Combo Box样式.当然,现在这样看不出第一种与第三种之间的区别,但是第二种与其他两种的区别是明显的,第二种的列表框始终是出于现实状态的. Combo Box: 一 ...

  2. HTML+CSS Day05 基本CSS选择器、复合CSS选择器与CSS继承性

    1.基本CSS选择器 (1)标记选择器 <style>                       h1{ color:red; font-size:25px;}           &l ...

  3. javaScript 新学习:Array.contains 函数

    Array.contains 函数 确定指定对象是否是 Array 对象中的元素. 此函数是静态的,可在不创建对象实例的情况下调用. var itemExists = Array.contains(a ...

  4. linux的学习系列 2--文件系统

    Linux中的所有数据都被保存在文件中,所有的文件被分配到不同的目录.目录是一种类似于树的结构,称为文件系统. 当你使用Linux时,大部分时间都会和文件打交道,通过本节可以了解基本的文件操作,如创建 ...

  5. POJ 2115 C Looooops (扩展欧几里德 + 线性同余方程)

    分析:这个题主要考察的是对线性同余方程的理解,根据题目中给出的a,b,c,d,不难的出这样的式子,(a+k*c) % (1<<d) = b; 题目要求我们在有解的情况下求出最小的解,我们转 ...

  6. hrbust oj 1025 (计算几何+近似计算)

    这是我第一次只要可以这么做,题目中给的精度范围较大,所以可以把圆形的区域直接用小方块拼接近似来表示,maps地图开的越大,精度越高,但同时耗时也更多. 代码如下: #include<cstdio ...

  7. Oracle Sql优化之Rownum的使用

    1.rownum:rownum是一个伪列,需要在数据取出来后,rownum才会有值,因此在分页查找时,需要进行嵌套查询. select sal,ename from (select rownum as ...

  8. dfs+dp思想的结合------hdu1078

    首先是题目的意思: 从一个正方形的0,0点开始走,只能横着走,竖着走,最多走k步,下一个点的数一定要比当前这个点的值大,每走一步,就加上下一个点的数据,问数据最大能有多少. 首先遇到这种题目,走来走去 ...

  9. Nginx配置proxy_pass【转载】

    在nginx中配置proxy_pass时,当在后面的url加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分也给代理走. 下面四种 ...

  10. Tomcat的class加载的优先顺序

    Tomcat的class加载的优先顺序一览 1.最先是$JAVA_HOME/jre/lib/ext/下的jar文件. 2.环境变量CLASSPATH中的jar和class文件. 3.$CATALINA ...