VB.net 中没有增量运算符
我对 vb.net 相当陌生,在将 C# 中的 for 循环转换为 VB.net 时遇到了这个问题 我意识到增量运算符在 vb.net 中不可用(++ 和 --) 虽然我能够做类似 cnt +=1
的事情,但
我研究了一下并发现了 Eric 的帖子也有同样的内容,但并没有真正完全理解它。 他提到在 VB 中,语句不能只是一个表达式。不确定它如何真正适合。
我希望这里有人能够解释为什么它不能以与它相同的方式工作在 C# 中是这样的。 (希望这也适用,就像我们在 C# 中使用 ==
进行比较一样)
I am fairly new to vb.net and came across this issue while converting a for loop in C# to VB.net
I realized that the increment operators are not available in vb.net (++ and --)
whereas i was able it do something like cnt +=1
I researched a bit and came across Eric's post on the same, but wasn't really able to understand fully on it.
He mentions of In VB, a STATEMENT cannot be just an EXPRESSION. not sure how that really fits in.
I hope someone here would be able to explain why this doesn't work in the same way as it does in C#.
(Hope this will also hold true as in why we have ==
in C# for comparison)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我想说的是,在设计 Visual BASIC 时,语言设计者只是认为 BASIC 是比 C 更好的基线。您可以通过
C++
、Java
和追踪
.C
(以及更早的BCPL
)的沿袭C#VB
谱系来自 Dartmouth 的原始BASIC
(以及更早的Fortran
),并且是完全不同的野兽。换句话说,最初作为令人尊敬的
BASIC
:可能已经被黑客攻击并破坏了足够 :-)
根据 Eric 的帖子,
i++;
是实际上只是一个表达式,它产生i
,其副作用是i
在事件发生后递增(类似于非副作用表达式i;< /代码>)。
这是因为
C
允许这些裸露的表达式,甚至是像42;
这样的东西,它并没有真正做太多事情,但完全有效。换句话说,下面是一个完整的C
程序:所有这些表达式都是有效但无用的(当然除了最后的
0
)。在
BASIC
中,这并没有真正完成,因为BASIC
由语句(做某事的事情)组成。这就是为什么i += 1
(一个递增i
的语句)被认为是可以的,但是i++
(一个什么也不做的表达式恰好有一个增加i
的副作用)不是。你可能会说这只是语义上的吹毛求疵,但事实就是如此。你应该感谢小小的怜悯,至少你不必处理 COBOL:
I would say that the language designers simply thought that BASIC was a better baseline than C, when designing Visual BASIC. You can follow the lineage of
C
(and, earlier,BCPL
) throughC++
,Java
andC#
.The
VB
lineage comes from the originalBASIC
from Dartmouth (and, earlier,Fortran
) and is a different beast altogether.In other words, what started as the venerable
BASIC
:has probably been hacked and destroyed enough :-)
As per Eric's post,
i++;
is indeed just an expression, one that yieldsi
with the side effect thati
is incremented after the event (similar to the non-side-effect expressioni;
).That's because
C
allows these naked expressions, even things like42;
which doesn't really do much but is perfectly valid. In other words, the following is a completeC
program:All those expressions are valid but useless (except the
0
at the end of course).In
BASIC
, this was not really done, becauseBASIC
consisted of statements (things that did something). That's whyi += 1
(a statement incrementingi
) is considered okay, buti++
(an expression doing nothing which just happens to have a side effect which incrementsi
) isn't. You could argue that it's just semantic hair-splitting but that's just the way it is.You should be thankful for small mercies, at least you're not having to deal with COBOL:
很简单,因为设计者认为当你有
i += 1
时,i++
是不必要的。For
循环不需要任何一个,因此您不会丢失任何内容。毕竟它是视觉Basic...为什么要让它变得复杂呢?
Simply because the designers thought that
i++
is unnecessary when you havei += 1
.For
loops don't need either one, so you don't lose anything.It's Visual Basic after all... why make it complicated?
正如 @paxdiablo 所说,在 VB 中(或者更确切地说,在它的祖先 BASIC 中),一切都曾经是一个语句。事实上,每条语句都是由一个关键字引入的。
因此,要分配我们拥有的变量
并调用方法,我们必须
在VB中,
LET
和CALL
最终被删除(除了在一种特殊情况下),因为它完全是多余的并且不会增加清晰度。但 VB 的底层词法语法并没有发生太大变化:每个语句仍然必须是一个语句。i++
不是 VB 中的语句,因为它缺少函数调用或赋值。在 VB.NET 的第一个版本中,是否要像 C# 那样引入前置和后置自增运算符存在争议。我们决定不这样做,原因很简单:无论如何都不建议在表达式中使用副作用。它通常会影响清晰度。因此,即使在 C# 中,在表达式中合法使用
i++
也非常罕见,而合法使用++i
则更加罕见(尽管我赢了)不可否认,在某些情况下它增加了清晰度)。在大多数情况下,您可以使用
i += 1
就可以了,这很好地表达了意图。请注意,在 C++ 中,情况根本不同,因为这里(但在 C# 中不是!)
i++
实际上与具有不同的语义 i += 1
由于运算符重载(在 C# 中我们也有运算符重载,但++
不能重载)。As @paxdiablo said, in VB (or rather, in its ancestor BASIC), everything used to be a statement. And in fact, every statement was introduced by a keyword.
So to assign a variable we had
and to call a method, we had
In VB, the
LET
andCALL
were finally dropped (except in one special circumstance) because it’s completely redundant and doesn’t add clarity. But the underlying lexical grammar of VB didn’t change all that much: each statement still has to be a statement.i++
isn’t a statement in VB, since it lacks either a function call or an assignment.There was an argument in the first version of VB.NET whether to introduce pre- and post-increment operators like in C#. It was decided not to do this, for a fairly simple reason: using side-effects in expressions isn’t recommended anyway. It usually lets clarity suffer. So even in C# legitimate uses of
i++
in an expression are very rare, and legitimate uses of++i
are rarer still (though I won’t deny that in some cases it adds clarity).In most cases you can use
i += 1
just fine and this perfectly well expresses the intent.Notice that in C++, the situation is fundamentally different because here (but not in C#!)
i++
actually has a different semantic thani += 1
due to operator overloading (in C# we also have operator overloading but++
cannot be overloaded).作为 VB 中表达式和语句之间区别的示例,
在 VB 中,以下代码会生成编译器错误,因为
count += 1
将count
加 1,但整个表达式count += 1
不会返回结果,因此不能用作参数。当然,这同样适用
于在字符串上使用
+=
运算符。“在 VB 中,语句不能只是表达式”是什么意思?
2 + 2
生成结果4
并且可以单独作为一条语句,而在 VB 中则不能。“为什么 VB 中不提供前置和后置增量运算符?”的简化答案
count++
表示,首先返回count
的值,然后增加count
(并且不将赋值的值返回给count
)。在这种情况下,不使用递增的值(使用递增之前的值)。如前所述,VB 编译器要求您使用运算或为其赋值。
++count
表示,首先递增count
,然后将赋值给count< /代码>。
在这种情况下,将 +1 赋给
count
的值作为表达式的值返回。如前所述,VB 中的赋值不会产生结果。因此,在 VB 中实现这些运算符会带来一些严重的痛苦。
As an example of the difference between expression and statement in VB,
in VB the following generates a compiler error since
count += 1
incrementscount
by 1, but the whole expressioncount += 1
does not return a result, so it can't be used as a parameter.You have to do this instead
Of course same applies to using the
+=
operator on a String.What does "In VB, a statement cannot just be an expression" mean?
2 + 2
for instance produces the result4
and can stand alone as a statement, whereas in VB it can't.Simplified answer to "Why are pre and post increment operators not available in VB?"
count++
says, first return the value ofcount
, then incrementcount
(and do not return the value of the assignment tocount
).In this case the incremented value is not used (the value before incrementing is used). As mentioned before, the VB compiler requires you use or assign values of operations.
++count
says, first incrementcount
, then return the value of the assignment tocount
.In this case, the value of assigning +1 to
count
is returned as the value of the expression. As mentioned before, assignments in VB do not produce a result.Thus there would be some serious pain implementing these operators in VB.
以下扩展方法复制
++x
x++
--x
x--
The following extension methods replicate
++x
x++
--x
x--