C# 如何检查两个值之一是否为 TRUE?
对于 C# 专家来说这应该是一个简单的问题。
我基本上想检查一个值或另一个值是否为 TRUE,代码如下:
if ((Boolean.Parse(staff.getValue("Male")) | Boolean.Parse(staff.getValue("Female")))
{
// is true
}
这是正确的吗?
谢谢
Should be a simple question for the C# experts here.
I basically want to check if one value or another is TRUE, a wild stab at the code is below:
if ((Boolean.Parse(staff.getValue("Male")) | Boolean.Parse(staff.getValue("Female")))
{
// is true
}
Is this correct?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果“恰好有一个”为真,那么它就是:
If EXACTLY ONE should be true then it is:
听起来您正在寻找逻辑“或”。
Sounds like you're looking for the logical OR.
使用
||
(双管道)、逻辑或。在 C# 语句表达式中,从左到右求值。在 OR 运算中,如果第一个表达式等于 true,则不会计算第二个表达式。
Use the
||
(double pipe), logical OR.In C# statement expressions are evaluated from left to right. In an OR operation, the second expression will not be evaluated if the first one equals true.
条件 OR 运算符
||
就是您所需要的如果第一个条件为
TRUE
,则不会检查第二个条件,因为结果显然将返回正确。
The conditional OR operator
||
is what you needIf the first condition is
TRUE
, then the second condition isn't checked since the outcome is obviously going to returnTRUE
.请注意,
TryParse
比Parse
工作得更快、更安全,因为在发生错误时不会引发异常。TryParse
返回布尔值,指示解析是否成功。因此,两种解析方法都应返回
true
并且仅在此之后 - 进行主要检查或
Note that
TryParse
works more fast and more safe then justParse
because doesn't throw an exception in case of error.TryParse
returns bool that indicates was parse successful or was not.So both parsing methods should return
true
and only after that - do the main checkor
要指示性别是否使用“true”值而不是“false”指定,
.. 将仅确定它是否是这些值之一,而不是对象
staff
是这些值中的哪一个。所以,以防万一这个问题是字面意义而不是抽象的例子,……
男性或女性……每个人都是其中之一。也许在你的问题中你想问的是这两种情况中的哪一种?在这种情况下,
或者简单地说,
To indicate whether a gender is specified with a value of "true" rather than "false",
.. will only determine whether it's one of those values, not which of those values the object
staff
is.So, just in case this question is literal and not an abstract example, ...
Male or Female .. everyone is one or the other. Perhaps in your question you meant to ask which of the two is the case? In that case,
Or simply,
这是一个类似的场景,但我正在检查三个或更多布尔值。
This is a similar scenario but I am checking for three or more bool values.
无论如何,需要进行一些异常检查。 Boolean.Parse() 方法获取一个字符串作为参数,并仅返回
true
或false
如果参数一旦去掉空格就等于“True”或“False”(注意大小写)。在任何其他情况下,该函数都会返回异常。假设
staff.getValue("Male")
和staff.getValue("Female")
的可能值正是这两个,那么简单的析取 (||
) 就足够了。如果可能有任何其他返回值,包括null
和空字符串,那么您必须检查异常或手动比较
A little exception checking is needed anyway. The Boolean.Parse() method gets a string as argument and returns either
true
orfalse
only if the argument, once stripped out of whitespace, is equal to "True" or "False" (note capitalization). In ANY other case the function returns an exception.Supposing that the possible values of
staff.getValue("Male")
andstaff.getValue("Female")
are exactly those two, then the simple disjunction (||
) is sufficient. If any other return value is possible, includingnull
and the empty string, then you have to check for exceptionsor compare manually