我最近注意到 ReSharper (2016.3) 现在说要删除 C# 语句中的冗余括号,我相信这会导致预期行为发生变化。这是声明:
private bool GetAutoAdjust( int catType )
{
return ( catType == ChargeTypeIds.Penalty && _appSettingRepository.AutoRemovePen ) ||
( catType == ChargeTypeIds.Interest && _appSettingRepository.AutoRemoveInt ) ||
( catType == ChargeTypeIds.Discount && _appSettingRepository.AutoRemoveDis );
}
真的相当于这个吗?
private bool GetAutoAdjust( int catType )
{
return catType == ChargeTypeIds.Penalty && _appSettingRepository.AutoRemovePen ||
catType == ChargeTypeIds.Interest && _appSettingRepository.AutoRemoveInt ||
catType == ChargeTypeIds.Discount && _appSettingRepository.AutoRemoveDis ;
}
与 SQL Server 之类的东西相比,C# 计算 OR 子句的方式可能是我的错误,但不是仅在表达式的左侧和右侧对表达式进行 OR 计算,或者它实际上是否预连接了所有的表达式评估 OR 条件之前的 AND 条件?
最佳答案
在 C# 中,运算符有一个优先顺序。来自 the docs您可以看到 &&
出现在 ||
之前,所以从技术上讲,Resharper 是正确的,那些括号是多余的。
话虽如此,如果您觉得保留它们更明显 - 然后保留它们。代码的可读性应始终是优先事项。
关于c# - ReSharper 并删除多余的括号。 C# 中的 OR 与 AND 逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42941250/