vb.net += 在 Visual Basic 中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14693984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does += mean in Visual Basic?
提问by HereToLearn_
I tried to google the answer for this but could not find it. I am working on VB.Net. I would like to know what does the operator += mean in VB.Net ?
我试图用谷歌搜索这个答案,但找不到。我在 VB.Net 上工作。我想知道 VB.Net 中的运算符 += 是什么意思?
回答by Steven Doggart
It means that you want to add the value to the existing value of the variable. So, for instance:
这意味着您希望将该值添加到变量的现有值中。因此,例如:
Dim x As Integer = 1
x += 2 ' x now equals 3
In other words, it would be the same as doing this:
换句话说,它与这样做相同:
Dim x As Integer = 1
x = x + 2 ' x now equals 3
For future reference, you can see the complete list of VB.NET operators on the MSDN.
为了将来参考,您可以在MSDN上查看 VB.NET 运算符的完整列表。
回答by Carlos Vergara
a += b
is equivalent to
相当于
a = a + b
In other words, it adds to the current value.
换句话说,它增加了当前值。
回答by It'sNotALie.
It is plus equals. What it does is take the same variable, adds it with the right hand number (using the + operator), and then assigns it back to the variable. For example,
它是加等于。它所做的是取相同的变量,将其与右手数字相加(使用 + 运算符),然后将其分配回变量。例如,
Dim a As Integer
Dim x As Integer
x = 1
a = 1
x += 2
a = a + 2
if x = a then
MsgBox("This will print!")
endif
回答by sharp12345
those 2 lines compiled produce the same IL code:
编译的那两行产生相同的 IL 代码:
x += 1
x += 1
and
和
x = x + 1
x = x + 1
回答by Person
Just makes code more efficient -
只是让代码更有效率——
Dim x as integer = 3
x += 1
x += 1
'x = 4
'x = 4
is the same as
是相同的
x = x + 1
x = x + 1
'x = 4
'x = 4
It can also be used with a (-):
它也可以与 (-) 一起使用:
x -= 1
' x = 2
' x = 2
Is the same as
是相同的
x = x - 1
'x = 2
'x = 2

