kis*_*pit 4 c# type-conversion
static void Main()
{
short x = 3;/* no need explicit casting (short)3 */
Console.WriteLine(Factorial(x));
}
static short Factorial(short x)
{
return x == 0 ?
(short)1 /* need explicit casting (short)1 */
:
(short)(x * Factorial((short)(x - 1)));
}
Run Code Online (Sandbox Code Playgroud)
为什么我需要short在三元运算符中对整数文字进行显式转换?
小智 5
类型一的(bool) ? (short) : (int)表达是int,即使(int)子表达式是恒定的并且的范围内short.
有一个特殊的异常,int可以隐式转换short为常量和范围内,但是你的完整表达式不是常量,因此异常不适用,语言设计者从不添加其他类似的异常,可能是因为它们只会很少有用.
| 归档时间: |
|
| 查看次数: |
289 次 |
| 最近记录: |