[译]Probable C# 6.0 features illustrated

时间:2023-03-08 17:50:14

原文: 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"
};