[译]Probable C# 6.0 features illustrated
原文: 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的更多相关文章
- IIS 7.0 Features and Vista Editions
原文 IIS 7.0 Features and Vista Editions Overview of IIS 7.0 differences Across Windows Vista Editions ...
- [译]AngularJS 1.3.0 开发者指南(一) -- 介绍
[译]AngularJS 1.3.0 开发者指南(一) -- 介绍 Angular是什么 ? AngularJS是一款针对动态web应用的结构框架. 它可以让像使用模板语言使用HTML, 并且可以扩展 ...
- [译]AngularJS 1.3.0 开发者指南(一) -- 介绍 (转)
http://www.cnblogs.com/lzj0616/p/6440563.html [译]AngularJS 1.3.0 开发者指南(一) -- 介绍 Angular是什么 ? Angular ...
- 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 ...
- 【译】Android 6.0 Changes (机翻加轻微人工校对)
Android 6.0 Changes In this document Runtime Permissions Doze and App Standby Apache HTTP Client Rem ...
- 【译】Selenium 2.0 WebDriver
Selenium WebDriver 注意:我们正致力于完善帮助指南的每一个章节,虽然这个章节仍然存在需要完善的地方,不过我们坚信当前你看到的帮助信息是精确无误的,后续我们会提供更多的指导信息来完 ...
- 【译】Flink + Kafka 0.11端到端精确一次处理语义的实现
本文是翻译作品,作者是Piotr Nowojski和Michael Winters.前者是该方案的实现者. 原文地址是https://data-artisans.com/blog/end-to-end ...
- (译)MySQL 8.0实验室---MySQL中的倒序索引(Descending Indexes)
译者注:MySQL 8.0之前,不管是否指定索引建的排序方式,都会忽略创建索引时候指定的排序方式(语法上不会报错),最终都会创建为ASC方式的索引,在执行查询的时候,只存在forwarded(正向)方 ...
- [译]初试C# 8.0
原文地址: https://blogs.msdn.microsoft.com/dotnet/2018/12/05/take-c-8-0-for-a-spin/ 初试C# 8.0 昨天我们宣布了Visu ...
随机推荐
- 前端打包/自动化构建工具:gulp
glup可以进行打包,并且可以实现类似/script/test-adsf123.js或者/script/test.js?v=asdf123 参考: http://www.ydcss.com/archi ...
- Servlet基础-手工编写第一个servlet
[手工编写第一个servlet] [步骤] 1.继承HttpServlet 2.重写doGet()或者doPost()方法 //这个doGet或者doPost方法取决用户提交的方式 3.在web.x ...
- hdu 3089 约瑟夫环
原来并不知道约瑟夫环还可以递推直接解orz 约瑟夫问题的递推公式: 设f[n]表示一共n个人,数到k出局,这样最后的winner (n个人从0开始标号,即0--n-1) f[n]=(f[n-1]+k) ...
- HTTP状态码查询
如果客户端向服务器发出了某项请求要求显示网站上的某个网页,那么,服务器会返回 HTTP 状态代码以响应该请求. 一些常见的状态代码为: 200 - 服务器成功返回网页 403 - 请求的网页禁止访问 ...
- 看看你的正则行不行——正则优化一般的json字符串
json字符串很有用,有时候一些后台接口返回的信息是字符串格式的,可读性很差,这个时候要是有个可以格式化并高亮显示json串的方法那就好多了,下面看看一个正则表达式完成的json字符串的格式化与高亮显 ...
- InternalsVisibleToAttribute——把internal成员暴露给指定的友元程序集
友元程序集简介 我们知道一个类中被定义为internal的成员(包括类型.方法.属性.变量.事件)是只能在同一个程序集中被访问到的(当然了,我这里说的是正常的方式,不包括通过反射来访问).这个规则在. ...
- MOOCULUS微积分-2: 数列与级数学习笔记 7. Taylor series
此课程(MOOCULUS-2 "Sequences and Series")由Ohio State University于2014年在Coursera平台讲授. PDF格式教材下载 ...
- mysql中max_allowed_packet参数的配置方法(避免大数据写入或者更新失败)
修改方法 1.修改配置文件 可以编辑my.cnf来修改(windows下my.ini),在[mysqld]段或者mysql的server配置段进行修改. 代码如下: max_allowed_packe ...
- js004-变量、作用域和内存问题
js004-变量.作用域和内存问题 4.1 基本类型和引用类型的值 基本类型:简单的数据段 引用类型:可能由多个值构成的对象 五种基本数据类型:undefined.null.boolean.Numbe ...
- spring-boot-cli