我应该在"其他"中包装递归步骤吗?

Ail*_*lyn 0 c# recursion coding-style

可能重复:
否则或返回?

考虑典型的递归函数:

public int Fact(n) 
{
    if (n < 2)
    {
        return 1;
    }
    else
    {
        return n * Fact(n-1);
    }
}
Run Code Online (Sandbox Code Playgroud)

以这种方式写这种方式之间有什么区别?:

public int Fact(n) 
{
    if (n < 2)
    {
        return 1;
    }

    return n * Fact(n-1);
}
Run Code Online (Sandbox Code Playgroud)

我更喜欢后者,特别是当递归步骤由许多代码行组成时.我不想添加不必要的缩进.

是否存在实际差异,或者这仅仅是一种风格偏好?

Thi*_*ter 7

这只是一种风格问题.通常,当then块的结尾返回时,我不使用else块.