繁体   English   中英

为什么 VB 中的 AND 语句会编译为 & 运算符?

[英]Why does the AND statement in VB compile to a & operator?

我正在尝试将 VB.NET 代码转换为 C#。我有以下内容:

If IsDataProperty(p) And (p.Name.StartsWith("ref_") = False) Then
 ...

如果我使用反编译器查看 C# 版本的样子,我会得到:

if (this.IsDataProperty(p) & !p.Name.StartsWith("ref_")) {
...

VB中的AND运算符编译为& C#运算符。
代码不应该与&&运算符一起使用:

 if (this.IsDataProperty(p) && !p.Name.StartsWith("ref_")) {
...

从逻辑上讲,在 VB 代码中,如果IsDataProperty(p)为假,则整个语句将为假。

VB.NET有短路专用关键字。

bool valueAnd = conditionA && conditionB;
bool valueOr = conditionA || conditionB;
Dim valueAnd As Boolean = conditionA AndAlso conditionB
Dim valueOr As Boolean = conditionA OrElse conditionB

And 在 VB.NET 中的等价物实际上是 &。 要获得 C# 的 &&,您应该在 VB.NET 中使用“AndAlso”。

https://learn.microsoft.com/en-us/do.net/visual-basic/language-reference/operators/andalso-operator

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM