c# 6.0 文法特性

來源:互聯網
上載者:User

標籤:TE   default   turn   highlight   tty   初始化   catch   console   ati   

namespace _6._0新特性{    using static _6._0新特性.Statics.StaticClass;    class Program    {        static void Main(string[] args)        {            // 特性01  靜態using 聲明            //之前的調用方式  類名 +方法名            _6._0新特性.Statics.StaticClass.TestFunc();            //  在加了  using static _6._0新特性.Statics.StaticClass; 引用之後,直接調用 ,如下            TestFunc();         }}
  // 特性02  運算式體方法,如下簡寫方式        //public string GetNewString(string str)        //{        //    return $"{str}newString";        //}        public string GetNewString(string str) => $"{str}newString";        //調用上面的方法        public string GetNewString1(string str) => GetNewString("");

  

 //特性3  Get 方法的寫法        private string _name;        public string Name => _name;
 //特性4 Set 方法 設定初始值        public string Age { get; set; } = "111";        public int Age1 { get; set; } = default(Int32);
  //特性05  唯讀自動屬性        private readonly string _sex;        public string Sex => _sex;
static void Main(string[] args)        {                  //特性06 nameof            Console.WriteLine($"{nameof(Main)},{nameof(args)}");  //輸出 Main,args}
 //特性07            //if (str != null)            //    return str.ToString() + "22";            //else return str;            // 等同於下面的文法            return str?.ToString() + "22";
  // 特性08 字典得初始化            var dic = new Dictionary<string, string>();            dic.Add("11", "11");            // 以下新的寫法            var dict = new Dictionary<string, string>            {                ["11"] = "22",                ["22"] = ""            };
 // 特性09  異常的過濾            try            {            }            catch (Exception ex) when (ex.GetType() == typeof(ArgumentException))            {               //             }            // 特性10   可以在  catch 中使用Await 關鍵字

 

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.