有没有更好的写法?在最近做了很多 JavaScript 之后,我觉得我对 C# 感到生疏了。这可以改进吗?
foreach (var item in this.CartItems)
{
if (item.EffectivePrice != null)
{
this.CartItems[this.CartItems.IndexOf(item)].EffectivePrice =
CurrencyHelper.GetLocalizedCurrency(item.EffectivePrice);
}
}
最佳答案
好吧,您可以使用 from
和 where
用 LINQ 查询语法编写它,但我不确定它是大变化;我更想知道查找是否不必要:
this.CartItems[this.CartItems.IndexOf(item)].EffectivePrice =
CurrencyHelper.GetLocalizedCurrency(item.EffectivePrice);
到:
item.EffectivePrice = CurrencyHelper.GetLocalizedCurrency(item.EffectivePrice);
除此之外,我不确定是否值得进行更改;我可能会将其保留为:
foreach (var item in this.CartItems) {
if (item.EffectivePrice != null) {
item.EffectivePrice = CurrencyHelper.GetLocalizedCurrency(item.EffectivePrice);
}
}
关于c# - 林克?重构foreach,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2115131/