标签:style blog http ar io color 使用 sp for
原文: http://damieng.com/blog/2013/12/09/probable-c-6-0-features-illustrated
===========================================
以更简短的方式写一个构造函数给私有变量赋值.
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;
}
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.
private readonly int x;
public int X { get { return x; } }
public int X { get; } = x;
public int XX { get; set; } = xx;
引入一个类的所有的公共静态方法 .
public double A { get { return Math.Sqrt(Math.Round(5.142)); } }
using System.Math;
...
public double A { get { return Sqrt(Round(5.142)); } }
public double Distance {
get { return Math.Sqrt((X * X) + (Y * Y)); }
}
public double Distance => Math.Sqrt((X * X) + (Y * Y));
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);
之前pararms只支持Array.
Do(someEnum.ToArray());
...
public void Do(params int[] values) { ... }
Do(someEnum);
public void Do(params IEnumerable<Point> points) { ... }
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()
Removes the need to create static factory methods to infer generic types. This is helpful with Tuples etc.
var x = MyClass.Create(1, "X");
...
public MyClass<T1, T2> Create<T1, T2>(T1 a, T2 b) {
return new MyClass<T1, T2>(a, b);
}
var x = new MyClass(1, "X");
int x;
int.TryParse("123", out x);
int.TryParse("123", out int x);
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";
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
标签:style blog http ar io color 使用 sp for
原文地址:http://www.cnblogs.com/irocker/p/4166364.html