VB.Net - Operators

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. VB.Net is rich in built-in operators and provides following types of commonly used operators −
Arithmetic Operators
Arithmetic Operators
Comparison Operators
Comparison Operators
Logical/Bitwise Operators
Logical/Bitwise Operators
Bit Shift Operators
Bit Shift Operators
Assignment Operators
Assignment Operators
Miscellaneous Operators
Miscellaneous Operators
This tutorial will explain the most commonly used operators.
Following table shows all the arithmetic operators supported by VB.Net. Assume variable A holds 2 and variable B holds 7, then −
Show Examples
Following table shows all the comparison operators supported by VB.Net. Assume variable A holds 10 and variable B holds 20, then −
Show Examples
Apart from the above, VB.Net provides three more comparison operators, which we will be using in forthcoming chapters; however, we give a brief description here.
Is Operator − It compares two object reference variables and determines if two object references refer to the same object without performing value comparisons. If object1 and object2 both refer to the exact same object instance, result is True; otherwise, result is False.
Is Operator − It compares two object reference variables and determines if two object references refer to the same object without performing value comparisons. If object1 and object2 both refer to the exact same object instance, result is True; otherwise, result is False.
IsNot Operator − It also compares two object reference variables and determines if two object references refer to different objects. If object1 and object2 both refer to the exact same object instance, result is False; otherwise, result is True.
IsNot Operator − It also compares two object reference variables and determines if two object references refer to different objects. If object1 and object2 both refer to the exact same object instance, result is False; otherwise, result is True.
Like Operator − It compares a string against a pattern.
Like Operator − It compares a string against a pattern.
Following table shows all the logical operators supported by VB.Net. Assume variable A holds Boolean value True and variable B holds Boolean value False, then −
Show Examples
We have already discussed the bitwise operators. The bit shift operators perform the shift operations on binary values. Before coming into the bit shift operators, let us understand the bit operations.
Bitwise operators work on bits and perform bit-by-bit operations. The truth tables for &, |, and ^ are as follows −
Assume if A = 60; and B = 13; now in binary format they will be as follows −
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A  = 1100 0011
We have seen that the Bitwise operators supported by VB.Net are And, Or, Xor and Not. The Bit shift operators are >> and << for left shift and right shift, respectively.
Assume that the variable A holds 60 and variable B holds 13, then −
Show Examples
There are following assignment operators supported by VB.Net −
Show Examples
Str1 &= Str2 is same as
Str1 = Str1 & Str2
There are few other important operators supported by VB.Net.
Show Examples
AddHandler Button1.Click,
AddressOf Button1_Click
 
Dim result As res
= Await AsyncMethodThatReturnsResult()
Await AsyncMethod()
MsgBox(GetType(Integer).ToString())
Dim add5 = Function(num As
   Integer) num + 5
'prints 10
Console.WriteLine(add5(5))
Dim num = 5
Console.WriteLine(If(num >= 0,
"Positive", "Negative"))
Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator −
For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.