C#6 的一些新語法

people類

    public class People
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        /// <summary>
        ///C#6新語法. :8.自動實現的屬性初始化器
        /// </summary>
        public DateTime BirthDay { get; set; } = DateTime.Now.AddYears(-22);

        /// <summary>
        /// C#6新語法:在屬性/方法裏使用Lambda表達式(Expression bodies on property-like function members)
        /// </summary>
        public string NameFormat => string.Format("姓名:{0}", "NameFormat");
        /// <summary>
        /// C#6新語法:在屬性/方法裏使用Lambda表達式(Expression bodies on property-like function members)
        /// </summary>
        public void Print() => WriteLine(NameFormat);
    }

1.導入靜態類:

以前的寫法:靜態類.express

            {
                Console.WriteLine("Hello!");
                Console.WriteLine($"以前的使用方式[靜態類.]:{Math.Max(2, 3)}"); 
}

C#6的寫法:性能

            //引入命名空間
            using static System.Math;
            using static System.Console;
            WriteLine("Hello!");
            WriteLine($"如今的使用方式引入命名空間,這邊直接調用:{ Max(2, 3)}");

2.字符串嵌入值

            #region 2.字符串嵌入值(String interpolation)
            {
                WriteLine($"Id:{people.Id}  姓名:{people.Name} 生日:{people.BirthDay.ToString("yyyy-MM-dd")}");
                
                WriteLine($"{(people.Age >= 25 ? "臘肉" : "鮮肉")}");

                WriteLine($"Id:{{{people.Id}}} 姓名:{{{people.Name}}}");
            }
            #endregion

3.空值運算符

            #region 3.空值運算符(Null-conditional operators) 可空判斷
            {
                int? iValue = null;
                string sValue = iValue?.ToString();//string sValue = iValue.ToString();不判斷會報錯
                iValue = Convert.ToInt32(sValue);
                WriteLine($"iValue:{iValue}");
                string sName = "";
                WriteLine($"sName:{sName?.ToString()}");
            }
            #endregion

4.對象初始化器

            #region 4.對象初始化器(Index Initializers)
            {
                //以前經過add方法,add keyvalue進去
                {
                    IDictionary<int, string> dictOld = new Dictionary<int, string>() { };
                    dictOld.Add(1, "M");
                    dictOld.Add(2, "A");
                    dictOld.Add(3, "X");
                    foreach (var keyValue in dictOld)
                    {
                        Console.WriteLine($"key:{keyValue.Key},value:{keyValue.Value}");
                    }
                }

                {
                    IDictionary<int, string> dictOld1 = new Dictionary<int, string>()
                    {
                        { 1,"M"},{ 2,"A"},{ 3,"X"}
                    };
                }

                {
                    //經過索引的方式給字段進行初始化
                    IDictionary<int, DateTime> dictNew = new Dictionary<int, DateTime>()
                    {
                        [1] = DateTime.Now,
                        [2] = DateTime.Now.AddDays(-1),
                        [3] = DateTime.Now.AddDays(-2)
                    };
                    foreach (var keyValue in dictNew)
                    {
                        Console.WriteLine($"key:{keyValue.Key},value:{keyValue.Value}");
                    }
                }
            }
            #endregion

5.異常過濾器

            #region 5.異常過濾器(Exception filters)
            {
                int iExceptionValue = 2;
                try
                {
                    Int32.Parse("S");//Parse類型強制轉換
                }
                catch (Exception e) when (iExceptionValue > 1)
                {
                    WriteLine(e.Message);
                }
            }
            #endregion

6.nameof表達式

            #region 6.nameof表達式 (nameof expressions)
            {
                /*好比咱們平時喜歡寫字符串的形式,若是項目愈來愈大,後期忽然哪一個不用了,
             * 使用字符串的形式維護起來就會很麻煩,用nameof就能夠很好的解決,最重要的是不會影響性能!*/
                WriteLine(nameof(people));
                WriteLine(nameof(people.Name));
            }
            #endregion

7.在屬性/方法裏使用Lambda表達式

        /// <summary>
        /// C#6新語法:在屬性/方法裏使用Lambda表達式(Expression bodies on property-like function members)
        /// </summary>
        public string NameFormat => string.Format("姓名:{0}", "NameFormat");
        /// <summary>
        /// C#6新語法:在屬性/方法裏使用Lambda表達式(Expression bodies on property-like function members)
        /// </summary>
        public void Print() => WriteLine(NameFormat);
            #region 7.在屬性/方法裏使用Lambda表達式(Expression bodies on property-like function members)
            {
                people.Print();
            }
            #endregion
相關文章
相關標籤/搜索