与条件运算符和逻辑运算符混淆 - VB.net

发布于 2024-12-04 07:24:26 字数 343 浏览 9 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葵雨 2024-12-11 07:24:26

等于运算符在 C# 中为 ==,在 VB 中为 =

if ( (a == 0 && b != null) || (a == 1 && c != null) )
    statement; // One single statement only

或者

if ( (a == 0 && b != null) || (a == 1 && c != null) ) {
    statement; // Any number of statements
}

这个在线转换工具将为您将其转换为VB:

If (a = 0 AndAlso b IsNot Nothing) OrElse (a = 1 AndAlso c IsNot Nothing) Then
    statement
End If

C# && 转换为 VB 中的 AndAlso
C# || 转换为 VB 中的 OrElse

使用这些运算符,一旦确定结果,评估就会停止。这称为“短路”评估。例如在 a && 中b 如果 afalse,则结果为 false,并且 b 不会评价。当评估有副作用时(例如执行数据库查询、引发事件或修改数据),这一点尤其重要。它在诸如 person != null && 等情况下也很有用。 person.Name == "Doe" 其中,如果第一项的计算结果为 false,则第二项将引发异常。


不使用短路求值的 VB AndOr 布尔运算符的等效项是 &| > 在 C# 中。这里所有的术语都会被评估。

If (a = 0 Or b = 0 And c = 0) Then
    statement
End If
if (a = 0 | b = 0 & c = 0) {
    statement;
}

The equals operator is == in C# and = in VB.

if ( (a == 0 && b != null) || (a == 1 && c != null) )
    statement; // One single statement only

or

if ( (a == 0 && b != null) || (a == 1 && c != null) ) {
    statement; // Any number of statements
}

This online conversion tool will convert it to VB for you:

If (a = 0 AndAlso b IsNot Nothing) OrElse (a = 1 AndAlso c IsNot Nothing) Then
    statement
End If

C# && translates to AndAlso in VB.
C# || translates to OrElse 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 be false if a is false, and b 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 these person != null && person.Name == "Doe" where the second would throw an exception if the first term evaluates to false.


The equivalent of the VB And and Or Boolean operators that do not use short-circuit evaluation are & and | in C#. Here all the terms will always be evaluated.

If (a = 0 Or b = 0 And c = 0) Then
    statement
End If
if (a = 0 | b = 0 & c = 0) {
    statement;
}
好倦 2024-12-11 07:24:26

vb.net等效物将

If (a = 0 AndAlso b IsNot Nothing) OrElse (a = 1 AndAlso c IsNot Nothing ) Then

在C#中注明,它应该是a == 0,而不是a = 0

neckout

The vb.net equivalent would be

If (a = 0 AndAlso b IsNot Nothing) OrElse (a = 1 AndAlso c IsNot Nothing ) Then

Note in c#, it should be a == 0 and not a = 0

Checkout this post with a comprehensive comparison.

荒岛晴空 2024-12-11 07:24:26

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) )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文