System :: Boolean变量上的三元运算符 [英] Ternary operator on System::Boolean variable
本文介绍了System :: Boolean变量上的三元运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在System :: Boolean中使用三元运算符?此示例代码始终返回true:
How to use ternary operator with System::Boolean? This sample code always returns true:
bool Test(Boolean^ value)
{
return value ? true : false;
}
推荐答案
回答我自己的过早问题,这段代码有效:
Answering my own premature question, this code works:
bool Test(Boolean^ value)
{
return (bool)value ? true : false;
}
编辑:更好(并且遵循汉斯和马特的建议),此代码有效更好:
better yet (and following Hans' and Matt's advice) this code works better:
bool Test(Boolean value)
{
return value ? true : false;
}
或者,因为布尔值和布尔值是可转换的,所以这也是很好的代码,这取决于自动转换发生在其他地方。除显示三元运算符外,示例几乎没有意义:
Or, because Boolean and bool are convertible this is also good code, which relies on automatic conversion that happens elsewhere. Example has very little sense except for showing the ternary operator:
bool Test(bool value)
{
return value ? true : false;
}
这篇关于System :: Boolean变量上的三元运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文