C# 6.0 11个新特性
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个新特性的更多相关文章
- 【C++11】新特性——auto的使用
[C++11]新特性——auto的使用 C++11中引入的auto主要有两种用途:自动类型推断和返回值占位.auto在C++98中的标识临时变量的语义,由于使用极少且多余,在C++11中已被删除.前后 ...
- 【C++11】新特性——Lambda函数
本篇文章由:http://www.sollyu.com/c11-new-lambda-function/ 文章列表 本文章为系列文章 [C++11]新特性--auto的使用 http://www.so ...
- 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.----------- ...
- 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. ---------- ...
- C++反射机制:可变参数模板实现C++反射(使用C++11的新特性--可变模版参数,只根据类的名字(字符串)创建类的实例。在Nebula高性能网络框架中大量应用)
1. 概要 本文描述一个通过C++可变参数模板实现C++反射机制的方法.该方法非常实用,在Nebula高性能网络框架中大量应用,实现了非常强大的动态加载动态创建功能.Nebula框架在码云的仓库地 ...
- Qt5 中对 C++11 一些新特性的封装
在 Qt5 中,提供更多 C++11 的特性支持,接下来我们将进行详细的说明. slots (槽) 的 Lambda 表达式 Lambda表达式 是 C++11 中的一个新语法,允许定义匿名函数.匿名 ...
- 【Qt开发】Qt5 中对 C++11 一些新特性的封装
C++11 是现在的 C++ 标准的名称,C++11 为 C++ 语言带来很多新特性. 而 Qt 4.8 是 Qt 首个在其 API 中开始使用一些新的 C++11 特性的版本,我之前写过一篇博文:C ...
- C# 11 的新特性和改进前瞻
前言 .NET 7 的开发还剩下一个多月就要进入 RC,C# 11 的新特性和改进也即将敲定.在这个时间点上,不少新特性都已经实现完毕并合并入主分支 C# 11 包含的新特性和改进非常多,类型系统相比 ...
- 有史来最大改变 Android 5.0十大新特性
有史来最大改变 Android 5.0十大新特性 2014.10.16 14:51:31 来源:腾讯数码作者:腾讯数码 ( 0 条评论 ) 距离Android系统上一次重大更新不到一年的时间,谷歌 ...
随机推荐
- SQL 合并列值和拆分列值
合并列值 表结构,数据如下: id value ----- ------ aa bb aaa bbb ccc 需要得到结果: id values ------ ----------- aa,bb aa ...
- API CLOUD 快捷键
常用快捷键有:Ctrl+Z:撤销Ctrl+N:创建项目或文件Ctrl+Shift+F:代码格式化(这个经常用,可以美化代码,也可以通过这个检查代码是否出错)Ctrl+/ :注释和反注释Alt+/:强制 ...
- JPA 系列教程4-单向一对多
JPA中的@OneToMany @Target({METHOD, FIELD}) @Retention(RUNTIME) public @interface OneToMany { Class tar ...
- bios自检时间长,显示0075错误
一amibios主板,只有一IDE接口,接一硬盘一光驱,每次启动时,在bios自检界面,在检测完usb设备后,都要等个那么一两分钟,这个时候,可以在屏幕的右下角看到有数字:0075 ,这就是错误代码. ...
- 百度地图API地点搜索-获取经纬度
分享一下地图上的地点搜索和鼠标点击获取地点经纬度,这些都是地图比较基本和实用的代码,其中还包括了根据用户IP进行地图的显示.改变地图上的鼠标样式.启用滚轮缩放等,算是半入门吧,其他的一些可以自己参考百 ...
- pkgmgmt: Comparison between different Linux Systems..
found this page.. already done by precedents.. installation: aptitude install apt-get install yum in ...
- 在win7/8/10鼠标右键添加带管理员权限的“在此处打开命令窗口”
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Drive\shell\runas]@="@shell32.dll,-8506 ...
- sql server 2005导出数据
*/ EXEC sp_configure 'show advanced options', 1 GO */ 配置选项 'show advanced options' 已从 0 更改为 1.请运 ...
- CodeForces--TechnoCup--2016.10.15--ProblemB--Bill Total Value(字符串处理)
Bill Total Value time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- 学习笔记——原型模式Prototype
原型模式,简单说就是具有一个克隆方法,外部可以直接使用此方法得到相应对象的拷贝对象. 比如哆啦A梦的复制镜,一照,就把物品拷贝了一份(虽然是镜子复制是相反的,这里就忽略这个细节了) C++中依靠拷贝构 ...