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中的用法相似