1. 程式人生 > >c# 6.0 語法特性

c# 6.0 語法特性

sharp name exception 過濾 def AD code using wait

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 語法特性