Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果我不';不要使用else条件_C# - Fatal编程技术网

C# 如果我不';不要使用else条件

C# 如果我不';不要使用else条件,c#,C#,这两个例子的区别是什么: if(firstchek) { if(second) { return here(); } else { return here(); } } 这是: if(firstcheck) { if(second) { return here(); } return here(); // else code without else } // code wi

这两个例子的区别是什么:

if(firstchek)
{
    if(second)
    {
     return here();
    }
    else
    {
    return here();
    }
}
这是:

if(firstcheck)
{
    if(second)
    {
     return here();
    }
      return here();
    // else code without else
}
// code without else
// else code is here 
     return here();

假设没有其他代码,那么在代码路径和执行内容方面就没有区别

一般来说,主要区别在于,在指定
else
子句时,只有当
if
中的表达式计算结果为false时,才会运行该子句。如果不指定,代码将始终运行

更新

double getPayAmount() {
    double result;
    if (_isDead) result = deadAmount();
    else {
        if (_isSeparated) result = separatedAmount();
        else {
            if (_isRetired) result = retiredAmount();
            else result = normalPayAmount();
        };
    }
    return result;
};
double getPayAmount() {
    if (_isDead) return deadAmount();
    if (_isSeparated) return separatedAmount();
    if (_isRetired) return retiredAmount();
    return normalPayAmount();
};  
这:

这是:

if(second)
{
 return here();
}
return here();
与此相同:

return here();
if (someCondition)
{
    foo();
}
else
{
    bar();
}
return;

为什么??因为无论第二个
的计算结果是什么,您都在做同样的事情,所以检查是多余的

对于第一个代码,无论第二个代码是否为定义值,您都在运行某些东西。这取决于它是否是一个定义的值。如果是,则运行一位代码。如果不是,那你就再跑一次。对于第二个示例,只有当second是定义的值时,才运行代码。

此代码:

if (someCondition)
{
    foo();
    return;
}
bar();
return;
与此相同:

return here();
if (someCondition)
{
    foo();
}
else
{
    bar();
}
return;
唯一的区别在于可读性。有时一种方法更具可读性,有时另一种方法更具可读性。看

嵌套条件语句

double getPayAmount() {
    double result;
    if (_isDead) result = deadAmount();
    else {
        if (_isSeparated) result = separatedAmount();
        else {
            if (_isRetired) result = retiredAmount();
            else result = normalPayAmount();
        };
    }
    return result;
};
double getPayAmount() {
    if (_isDead) return deadAmount();
    if (_isSeparated) return separatedAmount();
    if (_isRetired) return retiredAmount();
    return normalPayAmount();
};  
保护条款

double getPayAmount() {
    double result;
    if (_isDead) result = deadAmount();
    else {
        if (_isSeparated) result = separatedAmount();
        else {
            if (_isRetired) result = retiredAmount();
            else result = normalPayAmount();
        };
    }
    return result;
};
double getPayAmount() {
    if (_isDead) return deadAmount();
    if (_isSeparated) return separatedAmount();
    if (_isRetired) return retiredAmount();
    return normalPayAmount();
};  

在第一种情况下,如果第二个变量为false,则仅执行else部分。
在第二种情况下,始终执行省略else的部分(假设两种情况下firstcheck都为true)。

这两组代码在语义上相似。也就是说,它们将在运行时执行相同的操作。然而,您是否应该使用一种形式还是另一种形式取决于具体情况。除了所需的语义之外,您的代码还应该表达您的意图

如果代码的目的是执行其中一项或另一项,那么请保留else,以便您的意图是明确的。例如

if (withdrawAmmount < accountBalance)
{
    return Deduct();
}
else
{
    return ArrangeCredit();
}

可维护性,你应该考虑删除返回语句是否会改变行为和代码的基础上,一些白痴会这样做(可能是我)。p>


还应该注意的是,对于else,代码执行相同的操作,没有返回,但是没有返回省略else将导致代码的行为不同。

您的代码有太多的返回语句,例如,我觉得它们是重复的

if(firstchek) 
{ 
    if(second) 
    { 
     return here(); 
    } 
    else 
    { 
    return here(); 
    } 
} 
以上等于

if(firstchek) 
{         
   return here();         
} 
因为here()是同一个函数调用。第二个例子呢

 if(firstcheck)     
    {     
        if(second)     
        {     
         return here();     
        }     
          return here();     
        // else code without else     
    }     
    // code without else     
    // else code is here      
         return here();   
等于

if(firstcheck)     
{     
   return here();     
}
 return here(); 
在第一个示例中,如果在该示例之后有一些语句,并且顶层if条件失败,则将在该示例中执行它后面的语句

 if(firstchek) 
    { 
        if(second) 
        { 
         return here(); 
        } 
        else 
        { 
        return here(); 
        } 
    } 

CallMyCellNo();
如果顶级if条件失败,将调用CallMyCellNo()


在第二个示例中,在toplevel if语句之后有return here(),因此无论if条件的返回值如何,函数执行都将终止。

从技术上讲,您是正确的,但是这有点误导,因为注释//else代码没有else请告诉我如果我只返回而不执行任何操作action@4thpage-如果更新的示例符合您的意思,那么这两个示例并不相同,因为如果
firstCheck
为false,则第一个示例将不会返回。回答正确,尽管它随着更新而过时。若您所做的只是返回,那个么并没有什么区别。在第二个代码段中,您在
if
后面有代码-这似乎与您的问题无关。我也这么认为,因为当我运行这两个命令时,我没有发现任何差异。您也可以执行
返回第二个?here():here()如果你觉得特别棘手。对不起,问题是在我发布答案后编辑的。我现在已经更新了我的答案。