1.自動屬性增強
public bool ITuest { get; set; } = true;
public string FirstName { get; } = "aehyok";
2.拉姆表達式作爲函數體
public int Move(int x, int y) => x + y;
3.引用靜態類using static
public double ReturnNum() => Acos(3);
4.空值判斷null
int? length = customers?.Count;
int first = (int)customers?[0];
5.字符串嵌入值
public string MakeStr()
{
string firstName = "ligen";
string lastName = "wanli";
return string.Format("one is /{firstName},two is /{lastName}");
}
6.nameof表達式nameof expressions
public
static
void
AddCustomer(Customer customer)
{
if
(customer ==
null
)
{
throw
new
ArgumentNullException(
nameof(customer)
);
}
}
7.帶索引的對象初始化器Index initializers
var numer = new Dictionary<int, string> {[4] = "s33" };
8
try
{
res = await Resource.OpenAsync(…);
// You could do this. …
}
catch
(ResourceException e)
{
await Resource.LogAsync(res, e);
// Now you can do this …
}
finally
{
if
(res !=
null
)
await res.CloseAsync();
// … and this.
}
9、catch和finally 中的 await —— Await in catch and finally blocks
在C#5.0中,await關鍵字是不能出現在catch和finnaly塊中的。而在6.0中
try { res = await Resource.OpenAsync(…); // You could do this. … } catch (ResourceException e) { await Resource.LogAsync(res, e); // Now you can do this … } finally { if (res != null) await res.CloseAsync(); // … and this. }
public FractionStruct( int a, int b) { A = a; B = b; }
public FractionStruct() : this(0, 1) { }
結構體可以提供自定義的無參數構造函數了。
new FractionStruct()
default(FractionStruct)
new是調用無參數構造函數。
default是不調用無參數構造函數。
11.Null propagation 空傳播v?.A
v?["B"]
v?.ToString()
對象爲null時不調用屬性,索引器,方法等,表達式返回null,和Swift中的用法相似