谁能解释一下下面的代码 return total ?? decimal.Zero
好吗?
public decimal GetTotal()
{
// Part Price * Count of parts sum all totals to get basket total
decimal? total = (from basketItems in db.Baskets
where basketItems.BasketId == ShoppingBasketId
select (int?)basketItems.Qty * basketItems.Part.Price).Sum();
return total ?? decimal.Zero;
}
下面是什么意思?
if (total !=null) return total;
else return 0;
最佳答案
是的,就是这个意思。它叫做 null-coalescing operator .
这只是一个语法快捷方式。但是,它可以更有效,因为读取的值只计算一次。 (请注意,在两次计算值有副作用的情况下,也可能存在功能差异。)
关于c# 这一行是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10287972/