欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-04-18 13:35:22
从C#6开始,只读属性可简写为表达式属性。它使用双箭头替换了花括号,get访问器和return关键字。 例如: decimal CurrentPrice,sharedOwned; public decimal Worth { get{ return CurrentPrice*sharedOwned; ......

从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 
}