VB.Net - 賦值運算符

2022-06-02 13:53 更新

等價于VB.Net支持以下賦值運算符:

運算符描述示例
=簡單賦值操作符,將值從右側(cè)操作數(shù)分配給左側(cè)操作數(shù)C = A + B將A + B的值賦給C
+=加法賦值運算符,向左操作數(shù)添加右操作數(shù),并將結(jié)果賦給左操作數(shù)C += A等價于C = C + A
-=減法賦值運算符,它從左操作數(shù)中減去右操作數(shù),并將結(jié)果賦給左操作數(shù)C -= A 等價于 C = C - A
*=乘法賦值運算符,它將右操作數(shù)與左操作數(shù)相乘,并將結(jié)果賦給左操作數(shù)C *= A等價于 C = C * A
/=除法賦值運算符,它用右操作數(shù)劃分左操作數(shù),并將結(jié)果分配給左操作數(shù)(浮點除法)C /= A 等價于C = C / A
\=整除賦值運算符,它用右操作數(shù)劃分左操作數(shù),并將結(jié)果分配給左操作數(shù)(整數(shù)除法)C \= A 等價于 C = C \A
^=指數(shù)賦值運算符。 它將右操作數(shù)作為為左操作數(shù)的指數(shù)進行求冪,并將結(jié)果分配給左操作數(shù)C^=A等價于 C = C ^ A
<<=左移賦值運算符C <<= 2 等價于 C = C << 2
>>=右移賦值運算符C >>= 2 等價于 C = C >> 2
&=字符串連接操作符,可以將右邊的字符串連接到做字符串的后面(將左邊的字符串和右邊的字符串連接后賦值給左字符串)

Str1 &= Str2等價于

Str1 = Str1 & Str2

示例:
嘗試以下示例來了解VB.Net中可用的所有賦值運算符:
Module assignment
   Sub Main()
      Dim a As Integer = 21
      Dim pow As Integer = 2
      Dim str1 As String = "Hello! "
      Dim str2 As String = "VB Programmers"
      Dim c As Integer
      c = a
      Console.WriteLine("Line 1 - =  Operator Example, _
	  Value of c = {0}", c)
      c += a
      Console.WriteLine("Line 2 - +=  Operator Example, _
	  Value of c = {0}", c)
      c -= a
      Console.WriteLine("Line 3 - -=  Operator Example, _
	  Value of c = {0}", c)
      c *= a
      Console.WriteLine("Line 4 - *=  Operator Example, _
	  Value of c = {0}", c)
      c /= a
      Console.WriteLine("Line 5 - /=  Operator Example, _
	  Value of c = {0}", c)
      c = 20
      c ^= pow
      Console.WriteLine("Line 6 - ^=  Operator Example, _
	  Value of c = {0}", c)
      c <<= 2
      Console.WriteLine("Line 7 - <<=  Operator Example,_
	  Value of c = {0}", c)
      c >>= 2
      Console.WriteLine("Line 8 - >>=  Operator Example,_
	  Value of c = {0}", c)
      str1 &= str2
      Console.WriteLine("Line 9 - &=  Operator Example,_
	  Value of str1 = {0}", str1)
      Console.ReadLine()
   End Sub
End Module
當上述代碼被編譯和執(zhí)行時,它產(chǎn)生以下結(jié)果:
Line 1 - =  Operator Example, Value of c = 21
Line 2 - += Operator Example, Value of c = 42
Line 3 - -= Operator Example, Value of c = 21
Line 4 - *= Operator Example, Value of c = 441
Line 5 - /= Operator Example, Value of c = 21
Line 6 - ^= Operator Example, Value of c = 400
Line 7 - <<= Operator Example, Value of c = 1600
Line 8 - >>= Operator Example, Value of c = 400
Line 9 - &= Operator Example, Value of str1 = Hello! VB Programmers


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號