1. 自动属性的初始化

public string Name { get; set; } = "zhangsan";

2. 只读属性初始化

public string Name { get; } = "zhangsan";

3. 引用静态类Using Static

using static System.Math;//    引用命名空间

Abs(23);//    方法中调用Math.Abs绝对值的方式

 

4. 字符串嵌入值

var num = 25;
$"数字是{num}"        //    结果:数字是25

 

5. 用Lambda作为函数体

public int GetSumValue() => 1 + 2;    //    等同于    public int GetSumValue(){    return 1 + 2;    }

6. 用Lambda表达式用作属性

public int Value => 1 + 2;            //    等同于    public int Value{    get{    return 1+2;    }}

 

7. 带索引的对象初始化器Index initializers

public Dictionary<string, object> Dics() => new Dictionary<string, object>() { ["name"] = "duanlaibao@benlai.com" };
//    等同于
public Dictionary<string, object> Dics()
{
    Dictionary<string, object> Temp = new Dictionary<string, object>();
    Temp[“name”]= duanlaibao@benlai.com;
    return Temp;
}

 

8. 空值判断Null

var model = new PushObject();
model?.ID
//    等同于
    var model = new PushObject();
    if (model != null)
    {
        model.ID
    }

 

相关文章:

  • 2021-10-18
  • 2022-12-23
  • 2021-09-30
  • 2021-06-28
  • 2021-10-09
  • 2021-11-06
猜你喜欢
  • 2021-05-25
  • 2021-06-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-04
相关资源
相似解决方案