Operators

An operator is a symbol which has special meaning and performs an operation on single or multiple operands like addition, substraction etc. VB.net provides rich set of in-built operators.

Types of Operators in VB.net

1. Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations on operands.

OperatorDescriptionExample
+Used to perform Addition8+2 = 10
-Used to perform Subtraction12-2 = 10
*Used to perform Multiplication5*2 = 10
/Used to perform Division and returns float values10/3 = 3.3333
\Used to perform Division and returns integer100\10 = 10
MODUsed to return Remainder40 MOD 10 = 0
^Used to raise power of another operandx ^ y = x to the power of y

Example

Public Module Program
	Public Sub Main(args() As string)
        Dim x As Integer = 10
        Dim y As Integer = 2
        Dim sum As Integer 
        Dim diff As Integer 
        Dim product As Integer 
        Dim div As Integer 
        Dim mods As Integer 
        Dim power As Integer 
        
        sum = x + y
        Console.WriteLine("x + y: {0} ", sum)

        diff = x - y
        Console.WriteLine("x - y: {0} ", diff)

        product = x * y
        Console.WriteLine("x * y: {0} ", product)

        div = x / y
        Console.WriteLine("x / y : {0}", div)

        mods = x Mod y
        Console.WriteLine("x mod y: {0}", mods)

        power = x ^ y
        Console.WriteLine("x ^ y : {0}", power)

    End Sub
End Module

Check Result here

2. Relational Operators

VB.net relational operators are used to compare two operands.

OperatorDescriptionUsage
=Is equal tox == y
>Greater thanx > y
>=Greater than or equal tox >= y
<Less thanx < y
<=Less than or equal tox <= y

3. Bitwise Operators

VB.net bitwise operators are used to perform bitwise operations on operands.

OperatorDescription
ANDBitwise AND
ORBitwise OR
XORBitwise XOR
NOTBitwise NOT
AndAlsoLogical AND
OrElseLogical OR
isFalsechecks if given expression is false
isTruechecks if given expression is true

4. Bitwise Shift Operators

VB.net bitwise operators are used to perform bitwise operations on operands.

OperatorDescription
AndBinary AND
OrBinary OR
XorBinary XOR
NotBinary NOT
<<Binary Left Shift Operator
>>Binary Right Shift Operator

4. Logical operators

Below are the logical operators present in the VB.net.

OperatorDescriptionUsage
&&Logical AND(x > y) && (y > z)
||Logical OR(x > y) || (y > z)
!Logical NOT(!x)

5. Assignment Operators

Below are the assignment operators present in the VB.net.

OperatorDescriptionUsage
=Assignint x = 10;
+=Add and assignint x=10; x+=30; // x becomes 40
-=Subtract and assignint x=40; x-=10; // x becomes 30
*=Multiply and assignint x=10; x*=40; // x becomes 400
/=Divide and assignint x=100; x /= 10;// x becomes 10
=Divide and assignint x=100; x /= 10;// x becomes 10
%=Modulus and assignint x=100; x%=10; // x becomes 0
<<=Left shift and assignx <<= 2 is same as x = x << 2
>>=Right shift and assignx >>= 2 is same as x = x >> 2
&=concatenation and assignx &= 10 is same as x = x & 10
^=Exponentiation and assignx ^= 10 is same as x = x ^ 10

6. Miscellaneous Operators

OperatorDescription
AddressOfReturns the address
Awaitapplied to an operand while executing an asynchronous method
GetTypereturns type of a given object