Probable C# 6.0 features illustrated

來源:互聯網
上載者:User

標籤:style   blog   http   ar   io   color   使用   sp   for   

原文: 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 中使用 await12. 字典的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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.