原文: http://damieng.com/blog/2013/12/09/probable-c-6-0-features-illustrated

===========================================

1. 主构造函数

以更简短的方式写一个构造函数给私有变量赋值.

从前

public class Point {
private int x, y; public Point(int x, int y)
this.x = x;
this.y = y;
}
}

现在

public class Point(int x, int y) {
private int x, y;
}

Thoughts

  • Do you need to independently define x and y?
  • Can you still write a body?
  • How would you make the default private?

This solution feels too constrained, would have preferred something like:

public Point(set int x, set int y)

That set the property and optionally created a private one if it didn’t. Would allow bodies, use on multiple constructors etc.

2. 自动属性赋值

之前

private readonly int x;
public int X { get { return x; } }

现在

public int X { get; } = x;  
public int XX { get; set; } = xx;  

3. using 静态类 引入一个类的所有的公共静态方法;

引入一个类的所有的公共静态方法 .

之前

public double A { get { return Math.Sqrt(Math.Round(5.142)); } }

现在

using System.Math;
...
public double A { get { return Sqrt(Round(5.142)); } }

4. 属性表达式

之前

public double Distance {
get { return Math.Sqrt((X * X) + (Y * Y)); }
}

现在

public double Distance => Math.Sqrt((X * X) + (Y * Y));

思考

  • 尽管使用了 => 其实和System.Linq.Expression无关.

5. 方法表达式

之前

public Point Move(int dx, int dy) {
return new Point(X + dx1, Y + dy1);
}

现在

public Point Move(int dx, int dy) => new Point(X + dx, Y + dy);

6. Params 参数 支持的类型更多了

之前pararms只支持Array.

之前

Do(someEnum.ToArray());
...
public void Do(params int[] values) { ... }

现在

Do(someEnum);
public void Do(params IEnumerable<Point> points) { ... }

7. null 检查

之前

if (points != null) {
var next = points.FirstOrDefault();
if (next != null && next.X != null) return next.X;
}
return -1;

现在

var bestValue = points?.FirstOrDefault()?.X ?? -1;
int? first = customers?[0].Orders?.Count()

8. Constructor type parameter inference

Removes the need to create static factory methods to infer generic types. This is helpful with Tuples etc.

Before

var x = MyClass.Create(1, "X");
...
public MyClass<T1, T2> Create<T1, T2>(T1 a, T2 b) {
return new MyClass<T1, T2>(a, b);
}

After

var x = new MyClass(1, "X");

Thoughts

  • Another great addition.
  • Does it understand list and collection initializers to automatically determine the generic types too?

9. 就地声明输出参数

之前

int x;
int.TryParse("123", out x);

现在

int.TryParse("123", out int x);

10. 更优雅的string.Format

之前

var s = string.Format("{0} is {1} years{{s}} old", p.Name, p.Age);

现在

var s = "\{p.Name} is \{p.Age} year{s} old";
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";
var s = $"{p.Name} is {p.Age:D3} year{{s}} old";

11. 可以在catch finally 中使用 await

12. 字典的index Initializers

之前

var result = new Dictionary<string, string>()
{
  {"index1", "value1"},
  {"index2", "value2"}
};

现在

var result = new Dictionary<string, string>()
{
  ["index1"] = "value1",
  ["index2"] = "value2"
};
 

[译]Probable C# 6.0 features illustrated的更多相关文章

  1. IIS 7.0 Features and Vista Editions

    原文 IIS 7.0 Features and Vista Editions Overview of IIS 7.0 differences Across Windows Vista Editions ...

  2. [译]AngularJS 1.3.0 开发者指南(一) -- 介绍

    [译]AngularJS 1.3.0 开发者指南(一) -- 介绍 Angular是什么 ? AngularJS是一款针对动态web应用的结构框架. 它可以让像使用模板语言使用HTML, 并且可以扩展 ...

  3. [译]AngularJS 1.3.0 开发者指南(一) -- 介绍 (转)

    http://www.cnblogs.com/lzj0616/p/6440563.html [译]AngularJS 1.3.0 开发者指南(一) -- 介绍 Angular是什么 ? Angular ...

  4. C# 6.0 Features , C# 7.0 Features

    1 1 1 C# 6.0 Features http://stackoverflow.com/documentation/c%23/24/c-sharp-6-0-features#t=20160828 ...

  5. 【译】Android 6.0 Changes (机翻加轻微人工校对)

    Android 6.0 Changes In this document Runtime Permissions Doze and App Standby Apache HTTP Client Rem ...

  6. 【译】Selenium 2.0 WebDriver

    Selenium WebDriver   注意:我们正致力于完善帮助指南的每一个章节,虽然这个章节仍然存在需要完善的地方,不过我们坚信当前你看到的帮助信息是精确无误的,后续我们会提供更多的指导信息来完 ...

  7. 【译】Flink + Kafka 0.11端到端精确一次处理语义的实现

    本文是翻译作品,作者是Piotr Nowojski和Michael Winters.前者是该方案的实现者. 原文地址是https://data-artisans.com/blog/end-to-end ...

  8. (译)MySQL 8.0实验室---MySQL中的倒序索引(Descending Indexes)

    译者注:MySQL 8.0之前,不管是否指定索引建的排序方式,都会忽略创建索引时候指定的排序方式(语法上不会报错),最终都会创建为ASC方式的索引,在执行查询的时候,只存在forwarded(正向)方 ...

  9. [译]初试C# 8.0

    原文地址: https://blogs.msdn.microsoft.com/dotnet/2018/12/05/take-c-8-0-for-a-spin/ 初试C# 8.0 昨天我们宣布了Visu ...

随机推荐

  1. malware analysis、Sandbox Principles、Design && Implementation

    catalog . 引言 . sandbox introduction . Sandboxie . seccomp(short for secure computing mode): API级沙箱 . ...

  2. Android四大组件

    Activity 概念 活动是一种可以包含用户界面的组件,主要用于和用户交互.一个应用程序可以包含零个或多个活动. 基本用法 手动创建活动 1. 创建或加载布局 2. 在AndroidManifest ...

  3. AngularJs ngList、ngRepeat、ngModelOptions

    ngList 在文本输入的分隔的字符串和字符串数组间做转换,可以是一个固定的字符串分隔符(默认逗号)或正则表达式. 格式:ng-list=”value” value:表达式  通过这个值分隔字符串. ...

  4. SDUT 1400 马的走法(回溯法)

    题目链接: 传送门 马的走法 Time Limit: 1000MS     Memory Limit: 65536K 题目描述 在一个4*5的棋盘上,马的初始位置坐标(纵 横)位置由键盘输入,求马能返 ...

  5. SampleDateFormat进行日期格式化

    我们生成的日期 ,可能不是我们想要的格式,这时候,就要用到SampleDateFormat类的format方法转换一下, SampleDateFormat是java.text包下的一个常用日期类 这个 ...

  6. javascript之标识(zhi)符、关键字与保留字

    正确区分标识(zhi)符.关键字与保留字 我发现很多初学者往往弄不清楚这三者的区别,甚至会把标识符的“识(zhi)”读作识(shi),真是愧对小学的语文老师啊!!! 注意:在JavaScript中,所 ...

  7. 从Paxos到ZooKeeper-二、ZooKeeper和Paxos

    ZooKeeper为分布式应用提供了高效且可靠的分布式协调服务,提供了诸如tong'yi统一命名服务.配置管理和分布式锁等分布式的基础服务.在解决分布式数据一致性方面,ZooKeeper并没有直接采用 ...

  8. 从.o文件中提取指定开头依赖于外部接口的脚本

    nm -g audio_la-audio.o | grep " U " | awk '{ print $2}' | grep "^gst_"

  9. css003 选择器:明确设置哪些样式

    css003 选择器:明确设置哪些样式 1.每个样式的两个部分:选择器和声明块 1.标签选择器:整体控制 2.类选择器:精确控制(.+字母.数字.连字符或下划线) Css允许的类名为.+字母.数字.连 ...

  10. WinForm------SplitContainerControl的窗体调用控件方法

    Frm_Books窗体(窗体里面有个按钮跳转Frm_Book_Select窗体) private void Add_Book_ItemClick(object sender, DevExpress.X ...