c#各版本-新特性

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cqkxzyi/article/details/82994586

=====================================c#5.0========================================

  • 支持null类型运算
    int? x = null;
  • case支持表达式
    以前case里只能写一个具体的常量,而现在可以加表达式了

  • C#5.0两个新加的关键字async, await,异步处理更简化

=====================================c#6.0========================================

  • 自动属性初始化
    public string Name { get; set; } = "summit";
  • 字符串嵌入值
    Console.WriteLine($"年龄:{account.Age}");
    Console.WriteLine($"{(account.Age<=22?"小鲜肉":"老鲜肉")}");

  • 导入静态类
    using static System.Math;//注意这里不是命名空间哦
    Console.WriteLine($"之前的使用方式: {Math.Pow(4, 2)}");
    Console.WriteLine($"导入后可直接使用方法: {Pow(4,2)}");

  • 空值运算符
    var age = account.AgeList?[0].ToString();
    Console.WriteLine("{0}", (person.list?.Count ?? 0));

  • 对象初始化器
    IDictionary<int, string> dict = new Dictionary<int, string>() {
           [1]="first",
           [2]="second"      };

  • 异常过滤器
    private static bool Log(Exception e)
    {
        Console.WriteLine("log");
        return true;
    }
    static void TestExceptionFilter()
    {
        try
        {
            Int32.Parse("s");
        }
        catch (Exception e) when (Log(e))
        {
            Console.WriteLine("catch");
            return;
        }
    }
    当when()里面返回的值不为true,将持续抛出异常,不会执行catch里面的方法.

  • nameof表达式

  • 在cath和finally语句块里使用await

  • 在属性里使用Lambda表达式
    public string Name =>string.Format("姓名: {0}", "summit");
    public void Print() => Console.WriteLine(Name);

=====================================c#7.0========================================

  • out 变量
    StringOut(out string ddd); //传递的同时申明
    Console.WriteLine(ddd);
    
  • 元组Tuples
    var tuple = (1, 2);                    // 使用语法糖创建元组
    var tuple2 = ValueTuple.Create(1, 2);         // 使用静态方法【Create】创建元组
    var tuple3 = new ValueTuple<int, int>(1, 2);  // 使用 new 运算符创建元组
    WriteLine($"first:{tuple.Item1}, second:{tuple.Item2}, 上面三种方式都是等价的。");
    
    // 左边指定字段名称
      (int one, int two) tuple = (1, 2);
      WriteLine($"first:{tuple.one}, second:{tuple.two}");
      
      // 右边指定字段名称
      var tuple2 = (one: 1, two: 2);
      WriteLine($"first:{tuple2.one}, second:{tuple2.two}");
      
      // 左右两边同时指定字段名称
      (int one, int two) tuple3 = (first: 1, second: 2); 
      WriteLine($"first:{tuple3.one}, second:{tuple3.two}");
    
  • 解构元组
    (int one, int two) tuple = (1, 2);
    WriteLine($"first:{one}, second:{two}");
  • 匹配模式

object a = 1;

if (a is int c) //这里,判断为int后就直接赋值给c
{
  int d = c + 10;
  Console.WriteLine(d);
}

//下面是switch的新玩法
static dynamic Add(object a)
        {
            dynamic data;
            switch (a)
            {
                case int b when b < 0://还可以在此添加条件判断
                    data=b++;
                    break; 
                case string c:
                    data= c + "aaa";
                    break;
                default:
                    data = null;
                    break;
            }
            return data;
        }
  • 局部引用和引用返
  • 局部函数
    就是在函数体内部定义函数。

  • 扩展异步返回类型

    public async ValueTask<int> Func()
    {
           await Task.Delay(3000);
           return 100;
    }
  • 数值文字语法改进

    int a = 123_456;

  












 

猜你喜欢

转载自blog.csdn.net/cqkxzyi/article/details/82994586
今日推荐