C#6.0特性與vs2015

C#6.0 中的那些新特性

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
    }
相關文章
相關標籤/搜索