常用.Net 6.0 新特性

1、nameof表达式。Nameof表达式可以直接返回对象定义的名称,比如参数、枚举、变量、

控件、属性等。可以大大减少硬编码的使用,提高程序灵活性。

new GridColumn() { Caption = "工号", Field = nameof(Employee.EeJobno), DataType = typeof(string), Visible = true, Width = 100 },

2、字符串嵌入值($)。

MsgBox.Show(string.Format("查询所有模块失败:{0}",result.Message));

替换为

MsgBox.Show($"查询所有模块失败:{result.Message}");

 

public string FullName

   {

       get

       {

           return string.Format("{0} {1}", FirstName, LastName);

       }

   }

替换为

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

3Null 条件运算符

Null 值使代码变得复杂。 需要检查变量的每个访问,以确保没有取消对 null 的引用。Null 条件运算符使这些检查更轻松、更流畅。只需将成员访问.替换为 ?.。常用方式为属性、字段、方法等。可以简化一些空条件的判断

比如:

Queryed?.Invoke(this, list);

猜你喜欢

转载自www.cnblogs.com/yuanshuo/p/12010347.html