Mat*_*att 1 .net c# return-value
听起来有愚蠢的风险,为什么当条件块等于true时,以下方法返回结果而不是值1?
public long Recursive(int Input)
{
if (Input <= 1)
return 1;
else
return Input * Recursive(Input - 1);
}
Run Code Online (Sandbox Code Playgroud)
Ode*_*ded 11
它确实返回1 Input == 1.
但返回的1与先前调用一起使用,乘以Input,其返回值与先前调用一起使用,乘以Input,其返回值与先前调用一起使用,乘以Input,其返回值为与之前的通话一起使用,乘以Input......直到你回到第一次通话Recursive.
尝试查看Recursive使用该值调用时会发生什么3:
- input is not 1, so it calls Recursive with the value 2
- input is not 1, so it calls Recursive with the value 1
- input is 1, 1 is returned
- 2 * 1 is returned
- 3 * 2 is returned
| 归档时间: |
|
| 查看次数: |
4459 次 |
| 最近记录: |