茄子在线看片免费人成视频,午夜福利精品a在线观看,国产高清自产拍在线观看,久久综合久久狠狠综合

    <s id="ddbnn"></s>
  • <sub id="ddbnn"><ol id="ddbnn"></ol></sub>

  • <legend id="ddbnn"></legend><s id="ddbnn"></s>

    VS2015中C#版本6.0的新特性你需要知道
    來源:易賢網(wǎng) 閱讀:1745 次 日期:2016-08-06 14:36:32
    溫馨提示:易賢網(wǎng)小編為您整理了“VS2015中C#版本6.0的新特性你需要知道”,方便廣大網(wǎng)友查閱!

    本文列出個(gè)人感覺比較有用的幾個(gè)新功能,供大家參考,具體內(nèi)容如下 

    注意:這些新特性只能用于VS2015及更高版本,無法在VS2013、VS2010等低版本中使用。當(dāng)然,如果你不喜歡這些新的特性,仍然可以繼續(xù)使用原來的用法(所以說它是新的語法糖)。

    1、自動(dòng)屬性初始化的改進(jìn)(有用)

    原來的用法(聲明時(shí)無法同時(shí)初始化),例如:

    class MyClass

    {

      public int Age { get; set; }

      public string Name { get; set; }

      public MyClass()

      {

        Age = 20;

        Name = "張三";

      }

    新用法(聲明時(shí)可同時(shí)初始化,更方便了),例如:

    class MyClass

    {

      public int Age { get; set; } = 20;

      public string Name { get; set; } = "張三";

    2、String.Format的改進(jìn)(有用)

    原來的用法:用string.Format(…)實(shí)現(xiàn),例如:

    class MyClass

    {

      public void MyMethod()

      {

        string name = "張三";

        int age = 20;

        string s1 = string.Format("{0},{1}", name, age);

        string s2 = string.Format("姓名={0},年齡={1}", name, age);

        string s3 = string.Format("{0,15},{1:d3}", name, age);

        string s4 = string.Format("{0,15},{1,10:d3}", name, age);

        Console.WriteLine("{0},{1},{2},{3}", s1, s2, s3 ,s4);

        string s5 = string.Format("{0:yyyy-MM-dd}", DateTime.Now);

      }

    新用法:用“$”前綴實(shí)現(xiàn)(變量直接寫到大括號(hào)內(nèi),而且?guī)е悄芴崾?,更方便了),例如?nbsp;

    class MyClass

    {

      public void MyMethod()

      {

        string name = "張三";

        int age = 20;

        string s1 = $"{name},{age}";

        string s2 = $"姓名={name},年齡={age}";

        string s3 = $"{name,15},{age:d3}";

        string s4 = $"{name,15},{age,10:d3}";

        Console.WriteLine($"{s1},{s2},{s3},{s4}");

        string s5 = $"{DateTime.Now:yyyy-MM-dd}";

      }

    3、字典的初始化

    原來的用法: 

    class MyClass

    {

      public void MyMethod()

      {

        Dictionary<string, int> student = new Dictionary<string, int>();

        student.Add("a1", 15);

        student.Add("a2", 14);

        student.Add("a3", 16);

      }

    新用法(可以直接寫初始化的值,更方便了): 

    class MyClass

    {

      public void MyMethod()

      {

        Dictionary<string, int> student = new Dictionary<string, int>()

        {

          ["a1"] = 15,

          ["a2"] = 14,

          ["a3"] = 16

        };

      }

    4、可以用static聲明靜態(tài)類的引用

    原來的用法: 

    using System;

    namespace MyApp

    {

      class Demo1New

      {

        public static double MyMethod(double x, double angle)

        {

          return Math.Sin(x) + Math.Cos(angle);

        }

      }

    新用法(表達(dá)式比較復(fù)雜的時(shí)候有用,代碼更簡潔了):

    using static System.Math;

    namespace MyApp

    {

      class Demo1New

      {

        public static double MyMethod(double x, double angle)

        {

          return Sin(x) + Cos(angle);

        }

      }

    5、nameof表達(dá)式

    假定WPF應(yīng)用程序中有下面的類: 

    public class MyClass

     {

      

    public string MyText { get; set; } = "aaa";

      

    }

    并假定有下面的XAML代碼:

     <StackPanel>

    <TextBlock Name="txt1"/>

    ……

    </StackPanel>

    代碼隱藏類中原來的用法:

    txt1.SetBinding(TextBlock.TextProperty, "MyText"); 

    現(xiàn)在的用法(因?yàn)橛绣e(cuò)誤檢查智能提示,用起來更方便了):

    txt1.SetBinding(TextBlock.TextProperty, nameof(MyClass.MyText)); 

    6、Null-條件表達(dá)式

    (有用)

    var ss = new string[] { "Foo", null };

    var length0 = ss [0]?.Length; // 結(jié)果為3

    var length1 = ss [1]?.Length; // 結(jié)果為null

    var lengths = ss.Select (s => s?.Length ?? 0); //結(jié)果為[3, 0] 

    7、在try-catch-finally中使用await

    異步編程中,原來在catch或者finally中無法使用await,現(xiàn)在可以了: 

    async void SomeMethod()

    {

      try

      {

        //...etc...

      }

      catch (Exception x)

      {

        var diagnosticData = await GenerateDiagnosticsAsync (x);

        Logger.log (diagnosticData);

      }

      finally

      {

        await someObject.FinalizeAsync();

      }

    8、其他

    C# 6.0還有一些新的特性,對(duì)于初學(xué)者來說用的不是太多,所以這里就不再介紹了。

    再次說明一下,如果你不喜歡新的特性,仍然可以繼續(xù)使用原來的用法。

    以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助

    更多信息請(qǐng)查看網(wǎng)絡(luò)編程
    易賢網(wǎng)手機(jī)網(wǎng)站地址:VS2015中C#版本6.0的新特性你需要知道
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2026上岸·考公考編培訓(xùn)報(bào)班

    • 報(bào)班類型
    • 姓名
    • 手機(jī)號(hào)
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專用圖標(biāo)