您的位置:首页 > 编程语言 > C#

在C#6.0中的新语言特性

2015-06-05 17:14 489 查看
孙广东 2015.6.5
What's New in C# 6:
http://channel9.msdn.com/Shows/Visual-Studio-Toolbox/Whats-New-in-C-6

Cross Platform Development系列: http://channel9.msdn.com/Shows/CZSK-videa/Cross-Platform-Development-1-Introduction
Developer Productivity: What's New in C# 6系列: http://channel9.msdn.com/Series/Developer-Productivity-Whats-New-in-C-6/01
.NET Compiler Platform ("Roslyn") 的开源源代码: https://github.com/dotnet/roslyn
New Language Features in C# 6 文章: https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6

在C#6.0中的新语言特性:

这篇文章介绍在C#6.0中的新语言特性。所有这些都是可在VS 2015中执行。

1、Auto-property enhancements自动属性增强

1.1自动属性的初始值设定,现在您可以添加一个自动属性的初始值设定,就像字段:

public class Customer
{
public string First { get; set; } = "Jane";
public string Last { get; set; } = "Doe";
}

按写的顺序执行该初始值设定项,就像普通的字段初始值设定项一样,自动属性初始值设定项也不能引用this的

内容–毕竟他们在对象之前执行初始化。

1.2 只读属性的自动属性

public class Customer
{
public string First { get; } = "Jane";
public string Last { get; } = "Doe";
}
大家会好奇只读属性之前是怎么进行赋值的呢? 在构造函数中:
public class Customer
{
public string Name { get; };
public Customer(string first, string last)
{
Name = first + " " + last;
}
}

表达式--函数体成员(Expression-bodied function members):

Lambda表达式可以被声明为表达式体以及组成常规函数体块。此功能能带来同样的便利函数类型成员。

Expression bodies on method-like members:
public Point Move(int dx, int dy) => new Point(x + dx, y + dy);
public static Complex operator +(Complex a, Complex b) => a.Add(b);
public static implicit operator string(Person p) => p.First + " " + p.Last;

public void Print() => Console.WriteLine(First + " " + Last);

Expression bodies on property-like function members:
属性和索引器:
public string Name => First + " " + Last;
public Customer this[long id] => store.LookupCustomer(id);

Using static:

该功能允许导入所有要可访问类型的静态成员,使他们无需限定即可使用后面的代码中:
using static System.Console;
using static System.Math;
using static System.DayOfWeek;
class Program
{
static void Main()
{
WriteLine(Sqrt(3*3 + 4*4));
WriteLine(Friday - Monday);
}
}

这是伟大的,当你有一组相关功能的特定域,它随时使用。System.Math将是一个常见的例子。它还允许您直接访

问单个枚举类型的值,如System.DayOfWeek的成员。

Extension methods:

扩展方法是静态方法,但应作为实例方法使用。而不是global 范围的扩展方法,使用静态特性使该类型的扩展方

法为扩展方法可用:
using static System.Linq.Enumerable; // The type, not the namespace
class Program
{
static void Main()
{
var range = Range(5, 17); // Ok: not extension
var odd = Where(range, i => i % 2 == 1); // Error, not in scope
var even = range.Where(i => i % 2 == 0); // Ok
}
}

Null-conditional operators

有时代码往往要做null检查。空条件运算符允许您访问成员,只有当接收者是非null元素,否则提供一个空的结果:
int? length = customers?.Length; // null if customers is null
Customer first = customers?[0]; // null if customers is null

空条件运算符为了方便,与空合并运算符?? 一起使用:
int length = customers?.Length ?? 0; // 0 if customers is null

空条件操作符,具有短路的行为,也就是当前面的内容不为空的时候,后面紧跟着的链式成员才会被访问。
int? first = customers?[0].Orders.Count();
这个例子是本质上相当于:
int? first = (customers != null) ? customers[0].Orders.Count() : null;

当然null条件运算符本身可以被链接,如果有需要不止一次地检查null链中:
int? first = customers?[0].Orders?.Count();

请注意,?操作符后面的调用(用括号括起的参数列表)无法立即执行–这将导致太多的句法歧义。因此,直接调

用一个委托,如果它的存在,只有不工作的方式。但是,您可以通过调用委托方法:
if (predicate?.Invoke(e) ?? false) { … }

我们期望这种模式的一个非常普遍的使用将在触发事件上:
PropertyChanged?.Invoke(this, args);

String interpolation字符串插值:

String.Format 是非常灵活的和有用的,但他们使用的是有点笨拙而且容易出错。尤其不幸的是使用{0}等占位符

格式字符串,它必须单独行上提供的参数:
var s = String.Format("{0} is {1} year{{s}} old", p.Name, p.Age);

字符串插值,您可以把表达式在正确的地方,通过"holes"直接在字符串:
var s = $"{p.Name} is {p.Age} year{{s}} old";

就像String.Format ,可选的对齐方式和格式说明符可以得到:
var s = $"{p.Name,20} is {p.Age:D3} year{{s}} old";

内容可以几乎是任何表达式,甚至包括其他字符串:
var s = $"{p.Name} is {p.Age} year{(p.Age == 1 ? "" : "s")} old";

Notice that the conditional expression is parenthesized, so that the : "s" doesn’t get confused

with a format specifier.
请注意,是带圆括号的条件表达式,以便 : "s" 不混乱格式说明符。

nameof expressions:

(if x == null) throw new ArgumentNullException(nameof(x));
WriteLine(nameof(person.Address.ZipCode)); // prints "ZipCode"

Index initializers:

对象和集合初始值设定项可用于以声明方式初始化字段和属性,或给集合一组元素。使用索引器初始化字典和其

他对象不优雅。我们正在添加新对象初始值设定项的语法允许您通过任何索引器,它的新对象设置键的值:
var numbers = new Dictionary<int, string> {
[7] = "seven",
[9] = "nine",
[13] = "thirteen"
};

Exception filters

功能早VB 有. F#有. 现在C#中也有.
try { … }
catch (MyException e) when (myfilter(e))
{

}
private static bool Log(Exception e) { /* log it */ ; return false; }

try { … } catch (Exception e) when (Log(e)) {}

Await in catch and finally blocks:

Resource res = null;
try
{
res = await Resource.OpenAsync(…); // You could do this.

}
catch(ResourceException e)
{
await Resource.LogAsync(res, e); // Now you can do this …
}
finally
{
if (res != null) await res.CloseAsync(); // … and this.
}

Extension Add methods in collection initializers:
Improved overload resolution:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: