只是一个简单的问题,我可以在C#中像这样分配一个bool吗?
是行,
bool myBool = (theNumber > 0);
在C#中是否有效?
很明显,如果'theNumber'大于零,myBool会是真的吗?
只是一个简单的问题,我可以在C#中像这样分配一个bool吗?
是行,
bool myBool = (theNumber > 0);
在C#中是否有效?
很明显,如果'theNumber'大于零,myBool会是真的吗?
快速回答:您的代码没有任何问题。
bool myBool = (theNumber > 0);
有效,如果theNumber大于零,myBool将变为true。
请参考链接:
https://msdn.microsoft.com/en-us/library/c8f5xwh7.aspx
这里的例子使用:
bool b = true;
int days = ...;
// Assign the result of a boolean expression to b.
b = (days % 2 == 0);
希望帮助!
是的,这是有效的C#,当然提供theNumber
是一种数据类型,可以与数字进行比较。表达式theNumber > 0
的计算结果为布尔值(当theNumber
大于零时为true
),并且可以将其分配给布尔变量。
你不需要围绕价值的括号要么,但你可能要保持他们,如果你认为代码变得更具可读性:
bool myBool = theNumber > 0;
更快回答两个问题:是
当你尝试时发生了什么? –
...你需要做的只是翻转标志,看它是否有效/无效 – Plutonix
我知道VisualStudio可能需要很长时间才能启动,但我怀疑写这个问题比启动IDE快并按下F6。 –