【问题标题】:Is there any possibility to simplify the nested condition into non nested in C#?是否有可能将嵌套条件简化为 C# 中的非嵌套条件?
【发布时间】:2020-06-29 18:44:19
【问题描述】:

在返回真或假之前,我有 3 个条件需要检查。 这是表格:

这可以使用下面的逻辑来实现

if(!COND1 && !COND2)
{
    if(COND3)
    {
        return False
    }
    else
    {
        return True
    }
}
else
{
    return true
}

不使用嵌套 if 可以简化吗?

【问题讨论】:

  • 当然。该特定表减少为返回 (!cond1 || !cond2 || cond3)

标签: c# if-statement nested simplify


【解决方案1】:

你可以写:

if (a && b && !c)
{
    return false;
}
return true;

或者简单地说:

return !(a && b && !c)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-07
    • 1970-01-01
    • 1970-01-01
    • 2013-07-19
    • 1970-01-01
    • 2012-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多