C#6.0新特性

时间:2020-07-04 01:50:24   收藏:0   阅读:72

    1.什么是自动属性

      不需要定义字段 ,在编译时生产对应字段,相当于是微软提供的一个“语法糖”

        public int Age { get; set; } 

    2.只读自动属性

      使用访问修饰符修饰set

        public string Name { get; private set; } 

      也可以只申明get访问器

        public string Name { get;} 

    3.自动属性初始化

        public List<string> Names { get; set; } = new List<string>(); 

    4.使用表达式初始化

       public string FullName => $"{FirstName} {LastName}"; 

    用于导入单个类的静态方法  private double CalculateArea(double r) => PI * r * r; 

  1. 使用 ?. 替换如果result为null后面的ToList不会生效,返回值list为空
  2. ?? 当list不为空list2=list,如果为null则将??右侧赋值给list2
  1. 过滤器
      private void ExceptionFilter()
            {
                try
                {
                    
                }
                catch (Exception e) when(e.Message.Contains("400"))
                {
                    Console.WriteLine(e);
                    throw;
                }
            }

    在catch后使用when关键字进行条件筛选

  2. finally块中使用await
     private async void AwaitAtException()
            {
                try
                {
                    
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
                finally
                {
                    await Task.Delay(1000);
                }
            }

   nameof表达式的计算结果为符号的名称,例子返回值为“CSharp”

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!