c# 6.0 语法特性

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 关键字

 

posted @ 2018-05-30 21:37  谁说程序猿很猥琐  阅读(145)  评论(0编辑  收藏  举报