表达式属性(C#6.0和C#7.0

从C#6开始,只读属性可简写为表达式属性。它使用双箭头替换了花括号,get访问器和return关键字。 例如:

decimal CurrentPrice,sharedOwned;

public decimal Worth {

  get{

return CurrentPrice*sharedOwned;

}  }

使用表达式属性如下:

public decimal Worth=>CurrentPrice*sharedOwned;

C#7进一步允许在set访问器上使用表达式体,其书写方法如下:

public decimal Worth {    
get=>CurrentPrice*sharedOwned;   
 set=>sharedOwned=value 
}

猜你喜欢

转载自www.cnblogs.com/derong5093/p/12182701.html