Operators in C#
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. In C# there is a rich set of built-in operators which are shown below.
1-Arithmetic Operators
2-Relational Operators
3-Logical Operators
4-Bitwise Operators
5-Assignment Operators
6-Misc Operators
1-Arithmetic Operators
2-Relational Operators
Relation operators work upon the conditions
Operator
== If the values of two operands are equal or not, if yes then condition becomes true.
!= If the values of two operands are equal or not, if values are not equal then condition becomes true.
> If the value of the left operand is greater than the value of right operand, if yes then condition becomes true.
< If the value of the left operand is less than the value of right operand, if yes then condition becomes true.
>= If the value of the left operand is greater than or equal to the value of right operand, if yes then condition becomes true.
<= If the value of the left operand is less than or equal to the value of right operand, if yes then condition becomes true.
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. In C# there is a rich set of built-in operators which are shown below.
1-Arithmetic Operators
2-Relational Operators
3-Logical Operators
4-Bitwise Operators
5-Assignment Operators
6-Misc Operators
1-Arithmetic Operators
+ Addition
- Subtracts
* Multiplies
/ Divides
% Modulus
++ Increment
-- Decrement
Relation operators work upon the conditions
Operator
== If the values of two operands are equal or not, if yes then condition becomes true.
!= If the values of two operands are equal or not, if values are not equal then condition becomes true.
> If the value of the left operand is greater than the value of right operand, if yes then condition becomes true.
< If the value of the left operand is less than the value of right operand, if yes then condition becomes true.
>= If the value of the left operand is greater than or equal to the value of right operand, if yes then condition becomes true.
<= If the value of the left operand is less than or equal to the value of right operand, if yes then condition becomes true.
3-Logical operators
Logical operators specify how multiple search terms should be combined in a search query. The following table shows the logical operators AND, OR, NOT and NEAR.
Operators
1- && Logical AND operator.
If both the operands are non zero then condition becomes true.
2- || Logical OR Operator
If any of the two operands is non zero then condition becomes true.
3-! Logical NOT Operator.
It is used to reverse the logical state of its operand. If a condition is true then Logical NOT operator will make false.
4-Bitwise Operators
Bitwise operator works on bits and performs bit by bit operation.
For Example
1- & Binary AND Operator.
It copies a bit to the result if it exists in both operands.
2- | Binary OR Operator.
It copies a bit if it exists in either operand.
3- ^ Binary XOR Operator.
It copies the bit if it is set in one operand but not both.
4- ~ Binary One's Complement Operator.
It is unary and has the effect of 'flipping' bits.
5- << Binary Left Shift Operator.
The left operands value is moved left by the number of bits specified by the right operand.
6- >> Binary Right Shift Operator.
The left operands value is moved right by the number of bits specified by the right operand.
5-Assignment Operators
1- = Simple assignment operator.
It is used to Assigns values from right side operands to left side operand.
2- += Add AND assignment operator.
It is used to adds right operand to the left operand and assign the result to the left operand.
3- -= Subtract AND assignment operator.
It used to subtracts the right operand from the left operand and assign the result to the left operand.
5- *= Multiply AND assignment operator.
It used to multiplies the right operand with the left operand and assign the result to the left operand.
6- /= Divide AND assignment operator.
It is used to divides left operand with the right operand and assign the result to the left operand.
7- %= Modulus AND assignment operator.
It is used to takes modulus using two operands and assigns the result to the left operand.
8- <<= Left shift AND assignment operator.
9- >>= Right shift AND assignment operator.
10- &= Bitwise AND assignment operator
11- ^= bitwise exclusive OR and assignment operator.
12 - |= bitwise inclusive OR and assignment operator.
6-Miscellaneous Operators
1- sizeof() Returns the size of a data type.
2- typeof() Returns the type of a class.
3- & Returns the address of a variable.
4- * Pointer to a variable.
5- ? : Conditional Expression
6- is Determines whether an object is of a certain type.
7- as Cast without raising an exception if the cast fails.