c#6.0

c#6.0

一.字符串嵌入值:

$代替stirng.Format

二.自动属性赋值:

public string Name { get; set; } = "summit";
三.使用静态类导入using static System.Math;
Math类:Math.方法
新用法:直接方法名即可

四.空值校验:
之前
string name=null;
name.Tostring()会报错
现在
name?.Tostring()不会报错
五.对象初始化器:
之前
IDictionary<int, string> dictOld = new Dictionary<int, string>()
{
        { 1,"first"},
        { 2,"second"}
};
现在
直接通过索引赋值
IDictionary<int, string> dictNew = new Dictionary<int, string>()
 {
         [4] = "first",
         [5] = "second"
 };
六.异常:
之前:
catch (Exception e)
现在:
 catch (Exception e) when (exceptionValue > 1)//满足条件才进入catch

 catch (Exception e) when (exceptionValue > 1)//满足条件才进入catch
七.在属性/方法里面使用Lambda表达式
 public string NameFormat => string.Format("姓名: {0}", "summit");
2 public void Print() => Console.WriteLine(Name);





 

posted @ 2020-01-16 13:24  土豆哥  阅读(231)  评论(0编辑  收藏  举报