# VB.Net比較運算子

2019-10-16 23:02:27

`==` 檢查兩個運算元的值是否相等; 如果是，那麼條件為`True` `(A == B)`結果為：`False`
`<>` 檢查兩個運算元的值是否相等; 如果值不相等，則條件為`True` `(A <> B)`結果為：`True`
`>` 檢查左運算元的值是否大於右運算元的值; 如果是，則條件為`True` `(A > B)`結果為：`False`
`<` 檢查左運算元的值是否小於右運算元的值; 如果是，則條件為`True` `(A < B)`結果為：`True`
`>=` 檢查左運算元的值是否大於等於右運算元的值; 如果是，則條件為`True` `(A >= B)`結果為：`False`
`<=` 檢查左運算元的值是否小於等於右運算元的值; 如果是，則條件為`True` `(A <= B)`結果為：`True`

• `Is`運算子 - 它比較兩個物件參照變數，並確定兩個物件參照是否參照同一個物件而不執行值比較。 如果`object1``object2`都參照完全相同的物件範例，則結果為`True`; 否則，結果為`False`
• `IsNot`運算子 - 它還比較兩個物件參照變數，並確定兩個物件參照是否參照不同的物件。 如果`object1``object2`都參照完全相同的物件範例，則結果為`False`; 否則，結果為`True`
• `Like`運算子 - 它將字串與模式進行比較。

## 範例

``````Module comparison_operators
Sub Main()
Dim a As Integer = 21
Dim b As Integer = 10
If (a = b) Then
Console.WriteLine("Line 1 - a is equal to b")
Else
Console.WriteLine("Line 1 - a is not equal to b")
End If
If (a < b) Then
Console.WriteLine("Line 2 - a is less than b")
Else
Console.WriteLine("Line 2 - a is not less than b")
End If
If (a > b) Then
Console.WriteLine("Line 3 - a is greater than b")
Else
Console.WriteLine("Line 3 - a is not greater than b")
End If
' Lets change value of a and b '
a = 5
b = 20
If (a <= b) Then
Console.WriteLine("Line 4 - a is either less than or equal to  b")
End If
If (b >= a) Then
Console.WriteLine("Line 5 - b is either greater than  or equal to b")
End If
End Sub
End Module
``````

``````F:\worksp\vb.net\operators>vbc comparison_operators.vb
F:\worksp\vb.net\operators>comparison_operators.exe
Line 1 - a is not equal to b
Line 2 - a is not less than b
Line 3 - a is greater than b
Line 4 - a is either less than or equal to  b
Line 5 - b is either greater than  or equal to b
``````