VB.net中没有增量运算符

V4V*_*tta 28 c# vb.net operators

我对vb.net相当新,并且在将C#中的for循环转换为VB.net时遇到了这个问题我意识到增量运算符在vb.net(++和 - )中不可用,而我能够做到就像是 cnt +=1

我研究了一下,并发现了同样的Eric的帖子,但真的不能完全理解它.他提到在VB中,STATEMENT不能仅仅是一种表达.不确定这是如何真正适合的.

我希望这里的某个人能够解释为什么它不像C#中那样工作.(希望这也适用于为什么我们==在C#中进行比较)

pax*_*blo 34

我会说语言设计者只是认为这BASIC是比C设计时更好的基线Visual BASIC.您可以按照谱系C(以及更早BCPL)通过C++,JavaC#.

VB谱系来自于原来的BASIC达特茅斯(和,更早Fortran),并且是不同的野兽完全.

换句话说,最开始的是古老的BASIC:

LET I = I + 1
Run Code Online (Sandbox Code Playgroud)

可能已经被黑客入侵和破坏足够 :-)

根据Eric的帖子,i++;确实只是一个表达式,它产生i的副作用i在事件之后递增(i++;是一个表达式,就像非副作用表达式一样i;).

这是因为C允许这些裸体表达,即使是42;那些并没有真正做多少但完全有效的事情.换句话说,以下是一个完整的C程序:

int main (void) { 1; 2; 3; 4; 5; 6; 7; 8; 9; return 0; }
Run Code Online (Sandbox Code Playgroud)

所有这些表达都是有效但无用的.

BASIC,这并没有真正完成,因为BASIC陈述(做某事的事情)组成.这就是为什么i += 1(一个声明递增i)被认为是犹太教的但是i++(一个表达无所事事,恰好有副作用增加i)不是.你可以说它只是语义上的分裂,毫无疑问,VB设计师确实在争论.

但赢得这一天的团体是"我们C心爱的语言中不需要任何臭味"组.

你应该感谢小小的怜悯,至少你不必处理COBOL:

ADD 1 TO DD_WS_I.
Run Code Online (Sandbox Code Playgroud)

  • 我不太确定我是如何偶然发现这个问题的,但是答案很好. (2认同)

Meh*_*dad 8

仅仅因为设计师认为这i++是不必要的i += 1.

For 循环不需要任何一个,所以你不会丢失任何东西.

毕竟它是Visual Basic ...为什么让它变得复杂?

  • @ V4Vendetta:不,因为它被设计成类似于C/C++. (6认同)
  • 我不同意 - 你也不能在任何表达式上下文中使用`i += 1`,而在C++中,`++i`和`i += 1`可以是更大表达式的一部分(例如`a[+ +i] = (b += 2);`)。 (2认同)

Kon*_*lph 6

正如@paxdiablo所说,在VB中(或者更确切地说,在它的祖先BASIC中),一切都曾经是一个陈述.实际上,每个语句都是由关键字引入的.

所以要分配一个我们拥有的变量

LET x = x + 1
Run Code Online (Sandbox Code Playgroud)

我们有一个方法

CALL SomeMethod
Run Code Online (Sandbox Code Playgroud)

在VB中,LETCALL最终被丢弃(除了在一个特殊的情况下),因为它是完全多余的,不添加清晰度.但VB的基础词汇语法并没有发生太大变化:每个陈述仍然必须是一个陈述.i++不是VB中的语句,因为它缺少函数调用或赋值.

在VB.NET的第一个版本中有一个论点是否引入像C#中的前后增量运算符.由于一个相当简单的原因决定不这样做:不建议在表达式中使用副作用.它通常会让清晰度受损.因此,即使在C#i++ 中,表达式中的合法使用也是非常罕见的,合法使用的++i情况仍然很少(尽管我不会否认在某些情况下它会增加清晰度).

在大多数情况下,你可以使用i += 1得很好,这很好地表达了意图.

请注意,在C++中,这种情况是因为这里根本不同的(但不是在C#!)i++其实有着不同的语义i += 1由于操作符重载(在C#中我们也有运算符重载,但++不能超载).


Jas*_*n S 5

作为VB中表达式和语句区别的一个例子,在VB中下面会因为count += 1递增count1而产生编译器错误,但是整个表达式count += 1没有返回结果,所以它不能用作参数。

Dim count As Integer = 0
Console.WriteLine(count += 1)  ' compiler error
Run Code Online (Sandbox Code Playgroud)

你必须这样做

Dim count As Integer = 0
count += 1
Console.Writeline(count)
Run Code Online (Sandbox Code Playgroud)

当然,同样适用于+=在字符串上使用运算符。

“在 VB 中,语句不能只是表达式”是什么意思?

  • VB 编译器要求在某些赋值或其他操作中使用结果。
  • Because of this an assignment operation in VB does not produce a result. If it did the VB compiler would not allow it to stand alone as a statement (the compiler requires results be consumed).
  • Thus assignments in VB can be used as statements, but not as expressions. That is you cannot use an assignment statement as a parameter to a method, or as an intermediate result.
  • In C# an assignment operation does produce a value. Thus in order for assignments to stand alone as statements, the compiler does not require all results to be consumed.
  • The corollary in C# is that any other operation that produces a result can stand alone as a statement. 2 + 2 for instance produces the result 4 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 of count, then increment count (and do not return the value of the assignment to count).
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 increment count, then return the value of the assignment to count.
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.