原文: 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. Windows 8及以上系统安装好SQL Server 2008之后找不到SQL Server配置管理器的问题

    直接的方法: 打开[运行]->输入[C:\Windows\SysWOW64\mmc.exe /32 C:\Windows\SysWOW64\SQLServerManager10.msc]即可. ...

  2. 组合数取模Lucas定理及快速幂取模

    组合数取模就是求的值,根据,和的取值范围不同,采取的方法也不一样. 下面,我们来看常见的两种取值情况(m.n在64位整数型范围内) (1)  , 此时较简单,在O(n2)可承受的情况下组合数的计算可以 ...

  3. 解析:使用easyui的form提交表单,在IE下出现类似附件下载时提示是否保存的现象

    之前开发时遇到的一个问题,使用easyui的form提交表单,在Chrome下时没问题的,但是在IE下出现类似附件下载时提示是否保存的现象. 这里记录一下如何解决的.其实这个现象不光是easyui的f ...

  4. UEditor上传图片到七牛云储存(java)

    我们的网站一般放在虚拟空间或者服务器上,图片如果存在本地目录,会占用很多空间和流量,还增加了负担,好的办法是把图片存放到云储存服务里面,平时用url去拿 云储存:普遍说又拍云和七牛比较好,看到七牛免费 ...

  5. POJ 2386 Lake Counting(深搜)

    Lake Counting Time Limit: 1000MS     Memory Limit: 65536K Total Submissions: 17917     Accepted: 906 ...

  6. 【转载】Linux启动过程

    转自:http://cizixs.com/2015/01/18/linux-boot-process 简介 我们都知道:操作系统运行的代码是在硬盘上的,最终要跑到内存和 CPU 上,才能被我们使用. ...

  7. python从Microsoft Excel文件中导入数据

    excel中后缀为csv和xls,二者区别如下:1.xls 文件就是Microsoft excel电子表格的文件格式.2.csv是最通用的一种文件格式,它可以非常容易地被导入各种PC表格及数据库中. ...

  8. jQuery小技巧

    1. 禁止右键点击 $(document).bind("contextmenu",function(e){ return false; }); 2.隐藏搜索文本框文字 $(docu ...

  9. VMWare12虚拟CentOS7共享文件的过程

    环境: 宿主机:Win10企业版,虚拟机:VMware pro12.5,虚拟OS:CentOS7.0 过程: VMware菜单:虚拟机->设置->选项,选中宿主机要共享的磁盘或目录,点击确 ...

  10. ubuntu server设置时区和更新时间

    ubuntu server设置时区和更新时间 今天测试时,发现时间不对,查了一下时区: data -R    结果时区是:+0000 我需要的是东八区,这儿显示不是,所以需要设置一个时区   一.运行 ...