VS2015 C#6.0 中的那些新特性

來源:互聯網
上載者:User

標籤:

VS2015 C#6.0 中的那些新特性

前言

      VS2015在自己機器上確實是裝好了,費了老勁了,想來體驗一下跨平台的快感,結果被微軟狠狠的來了一棒子了,裝好了還是沒什麼用,應該還需要裝Xarmain外掛程式,配置一些參數吧,由於這塊之前從未接觸過,想了想還是先不把時間繼續浪費在這裡了,於是乎來體驗一下新特性了。

本人個人部落格原文連結地址為http://aehyok.com/Blog/Detail/66.html。

    本文參考http://roslyn.codeplex.com,參考PDF文檔http://files.cnblogs.com/aehyok/VS2015CSharp6.0.pdf

1、自動屬性的增強

1.1、自動屬性初始化 (Initializers for auto-properties)

C#4.0下的果斷實現不了的。

C#6.0中自動屬性的初始化方式

只要接觸過C#的肯定都會喜歡這種方式。真是簡潔方便呀。

 

 1.2、唯讀屬性初始化Getter-only auto-properties

先來看一下我們之前使用的方式吧

    public class Customer    {        public string Name { get; }        public Customer(string firstName,string lastName)        {            Name = firstName +" "+ lastName;        }    }

再來看一下C#6.0中

    public class Customer    {        public string FirstName { get; }="aehyok";        public string LastName { get; }="Kris";    }

和第一條自動屬性初始化使用方式一致。

2、Expression bodied function members

2.1 用Lambda作為函數體Expression bodies on method-like members

public Point Move(int dx, int dy) => new Point(x + dx, y + dy);  

再來舉一個簡單的例子:一個沒有傳回值的函數

public void Print() => Console.WriteLine(FirstName + " " + LastName);

 

2.2、Lambda運算式用作屬性Expression bodies on property-like function members

        public override string ToString()        { return FirstName + " " + LastName;        }

現在C#6中

    public class User    { public string FirstName { get; set; } public string LastName { get; set; } public override string ToString() => string.Format("{0}——{1}", FirstName, LastName); public string FullName => FirstName + " " + LastName;    }

 

3、引用靜態類Using Static

 在Using中可以指定一個靜態類,然後可以在隨後的代碼中直接使用靜態成員

 

4、空值判斷Null-conditional operators

 直接來看代碼和運行結果

 通過結果可以發現返回的都為null,再也不像以前那樣繁瑣的判斷null勒。

 

5、字串嵌入值    

在字串中嵌入值

之前一直使用的方式是

現在我們可以簡單的通過如下的方式進行拼接

6、nameof運算式nameof expressions

 在方法參數檢查時,你可能經常看到這樣的代碼(之前用的少,這次也算學到了)

        public static void AddCustomer(Customer customer)        { if (customer == null)            { throw new ArgumentNullException("customer");            }        }

裡面有那個customer是我們手寫的字串,在給customer改名時,很容易把下面的那個字串忘掉,C#6.0 nameof幫我們解決了這個問題,看看新寫法

        public static void AddCustomer(Customer customer)        { if (customer == null)            { throw new ArgumentNullException(nameof(customer));            }        }

 

7、帶索引的對象初始化器Index initializers 

 直接通過索引進行對象的初始化,原來真的可以實現

通過這種方式可以發現字典中只有三個元素,所以也就只有這三個索引可以訪問額,其他類型的對象和集合也是可以通過這種方式進行初始化的,在此就不進行一一列舉了。

8、異常過濾器 (Exception filters)

先來看一個移植過來的方法

            try { var numbers = new Dictionary<int, string> {[7] = "seven",[9] = "nine",[13] = "thirteen" };            } catch (ArgumentNullException e)            { if (e.ParamName == "customer")                {                    Console.WriteLine("customer can not be null");                }            }

在微軟的文檔中還給出了另一種用法,這個異常會在日誌記錄失敗時拋給上一層調用者

        private static bool Log(Exception e)        { ///處理一些日誌 return false;        } static void Main(string[] args)        { try { ///  } catch (Exception e){if (!Log(e))                {                }            }            Console.ReadLine();        }

 

9、catch和finally 中的 await —— Await in catch and finally blocks

 在C#5.0中,await關鍵字是不能出現在catch和finnaly塊中的。而在6.0中

            try {                res = await Resource.OpenAsync(…); // You could do this. …   } catch (ResourceException e)            { await Resource.LogAsync(res, e); // Now you can do this …  } finally { if (res != null) await res.CloseAsync(); // … and this.  } 

 

10、無參數的結構體建構函式—— Parameterless constructors in structs

 

總結

之前看到有大神發過一篇文章http://www.cnblogs.com/henryzhu/p/new-feature-in-csharp-6.html,自己還是禁不住想來切身的體驗一番。感覺很不錯。 也學到了不少新東西。

VS2015 C#6.0 中的那些新特性

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.