我正在尝试将 VB.NET 代码转换为 C#。我有以下内容:
If IsDataProperty(p) And (p.Name.StartsWith("ref_") = False) Then
...
Run Code Online (Sandbox Code Playgroud)
如果我使用反编译器查看 C# 版本的样子,我会得到以下结果:
if (this.IsDataProperty(p) & !p.Name.StartsWith("ref_")) {
...
Run Code Online (Sandbox Code Playgroud)
VB中的运算符AND
编译为&
C#运算符。
代码不应该与&&
运算符一起使用吗:
if (this.IsDataProperty(p) && !p.Name.StartsWith("ref_")) {
...
Run Code Online (Sandbox Code Playgroud)
从逻辑上讲,在VB代码中,如果IsDataProperty(p)
为假,则整个语句将为假。
VB.NET 有用于短路的特殊关键字。
bool valueAnd = conditionA && conditionB;
bool valueOr = conditionA || conditionB;
Run Code Online (Sandbox Code Playgroud)
Dim valueAnd As Boolean = conditionA AndAlso conditionB
Dim valueOr As Boolean = conditionA OrElse conditionB
Run Code Online (Sandbox Code Playgroud)