Eve*_*lie 89
int i = 0;
bool b = Convert.ToBoolean(i);
Run Code Online (Sandbox Code Playgroud)
Cor*_*rak 77
我假设0手段false(许多编程语言就是这种情况).这意味着true是not 0(有些语言使用-1一些其他人使用1;不伤害是要么兼容).所以你可以写:
bool boolValue = intValue != 0;
Run Code Online (Sandbox Code Playgroud)
开玩笑,如果您只希望输入的整数是 0 或 1,那么您真的应该检查一下是否是这种情况。
int yourInteger = whatever;
bool yourBool;
switch (yourInteger)
{
case 0: yourBool = false; break;
case 1: yourBool = true; break;
default:
throw new InvalidOperationException("Integer value is not valid");
}
Run Code Online (Sandbox Code Playgroud)
开箱即用的Convert不会检查这个;也不会yourInteger (==|!=) (0|1)。