与条件运算符和逻辑运算符混淆 - VB.net
我对 VB.net 有点陌生,而且由于我刚刚完成 C# 课程,缺少括号对如何编写某些运算符组合造成了很多混乱。
我试图在 VB 中重现的行的 C# 等效项是这样的:
if ( (a == 0 && b != null) || (a == 1 && c != null) )
我不知道如何在 VB 中编写此代码,我已经尝试了 And、Or、AndAlso、OrElse 等的多种组合,但我达不到想要的结果。
我找不到任何关于 C# 与 VB.net 运算符比较的清晰示例,而且我的注释也没有帮助。
有人可以帮我解决这个问题吗?
I'm kind of new to VB.net, and since I just finished a C# course, the lack of parentheses creates a lot of confusion on how to write certain combinations of operators.
The C# equivalent of the line I am trying to reproduce in VB would be like this :
if ( (a == 0 && b != null) || (a == 1 && c != null) )
I'm have no idea how to write this in VB, I've tried many combinations of And, Or, AndAlso, OrElse, etc. but I can't achieve the desired result.
I can't find any clear example of C# v.s. VB.net comparison on operators, and the notes I have aren't helpful either.
Can someone help me figure this out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
等于运算符在 C# 中为
==
,在 VB 中为=
。或者
这个在线转换工具将为您将其转换为VB:
C#
&&
转换为 VB 中的AndAlso
。C#
||
转换为 VB 中的OrElse
。使用这些运算符,一旦确定结果,评估就会停止。这称为“短路”评估。例如在
a && 中b
如果a
为false
,则结果为false
,并且b
不会评价。当评估有副作用时(例如执行数据库查询、引发事件或修改数据),这一点尤其重要。它在诸如person != null && 等情况下也很有用。 person.Name == "Doe"
其中,如果第一项的计算结果为false
,则第二项将引发异常。不使用短路求值的 VB
And
和Or
布尔运算符的等效项是&
和|
> 在 C# 中。这里所有的术语都会被评估。The equals operator is
==
in C# and=
in VB.or
This online conversion tool will convert it to VB for you:
C#
&&
translates toAndAlso
in VB.C#
||
translates toOrElse
in VB.With these operators the evaluation stops as soon as the result is determined. This is known as "short-circuit" evaluation. E.g. in
a && b
the result is known to befalse
ifa
isfalse
, andb
will not be evaluated. This is especially important when the evaluation has side effects, like performing database queries, raising events or modifying data. It is also useful in conditions like theseperson != null && person.Name == "Doe"
where the second would throw an exception if the first term evaluates tofalse
.The equivalent of the VB
And
andOr
Boolean operators that do not use short-circuit evaluation are&
and|
in C#. Here all the terms will always be evaluated.vb.net等效物将
在C#中注明,它应该是
a == 0
,而不是a = 0
neckout
The vb.net equivalent would be
Note in c#, it should be
a == 0
and nota = 0
Checkout this post with a comprehensive comparison.
if ( (a = 0 && b != null) || (a = 1 && c != null) )
等价于:
if ( ( a = 0 AndAlso b IsNot Nothing) OrElse (a = 1 而且也不是没有) )
if ( (a = 0 && b != null) || (a = 1 && c != null) )
Is equivilent to:
if ( ( a = 0 AndAlso b IsNot Nothing) OrElse (a = 1 AndAlso c IsNot Nothing) )