【发布时间】:2021-04-24 22:13:54
【问题描述】:
你能在 set 子句中做一个计算吗?然后在实施时返回总数?`
public decimal TotalCost
{
set
{ this.costTotal = (decimal)prodCost + (decimal)shipping + (decimal)insurance)}
get
{ return this.costTotal}
}
【问题讨论】:
-
是的,有可能。请记住,
costTotal仅在您使用 setter 时计算。但是,当您不使用 setter 中的value变量时,为什么还要对此类属性使用 setter?那没有意义。也许您正在寻找一个只有 getter 的属性,它可以即时计算值。 -
public decimal TotalCost => (decimal)prodCost + (decimal)shipping + (decimal)insurance);(没有二传手)。
标签: c# calculation