About Lesson
An operator is a symbol that instructs the compiler to carry out particular logical or mathematical operations. In C#, we the following categories of operators:
- Arithmetic Operators
- Comparison/Relational Operators
- Logical Operators
- Assignment Operators
- Bitwise Operators
- Misc Operators
Arithmetic Operators
# | Operator | Name | Description | Example |
---|---|---|---|---|
1 | + | Addition | Adds two values | A+B |
2 | – | Subtraction | Subtracts second value from first | A-B |
3 | * | Multiplication | Multiplies two values | A*B |
4 | / | Division | Divides numerator by denominator | A/B |
5 | % | Modulus | Remainder after an integer division | A%B |
6 | ++ | Increment | Increases the value of a variable by 1 | A++ |
7 | — | Decrement | Decreases the value of a variable by 1 | A– |
Comparison/Relational Operators
# | Operator | Name | Description | Example |
---|---|---|---|---|
1 | == | Equal to | Checks if the two values are equal | A==B |
2 | != | Not equal | Checks if the two values are not equal | A!=B |
3 | < | Less than | Checks if the left value is less than the right | A<B |
4 | > | Greater than | Checks if the left value is greater than the right | A>B |
5 | <= | Less than or equal to | Checks if the left value is less than or equal to the right | A<=B |
6 | >= | Greater than or equal to | Checks if the left value is greater than or equal to the right | A>=B |
Logical Operators
# | Operator | Name | Description | Example |
---|---|---|---|---|
1 | && | Logical AND | Checks if BOTH values are non zero. Returns True if both values/expressions evaluate to True | A&&B |
2 | || | Logical OR | Checks if ANY of the values is non zero. Returns True if at least one of the values/expressions evaluates to true | A!=B |
3 | ! | Logical NOT | Reverses the logical state of its conditional expressions. True becomes false and vice versa | !(A&&B) |
Assignment Operators
# | Operator | Name | Description | Example | Equivalent |
---|---|---|---|---|---|
1 | = | Assignment | Assigns values from right hand side of the equal sign to left hand side | A=B+C | Assigns value of B+C into A |
2 | += | Add AND assignment | Adds right value to the left and assigns the result to left value | A+=B | A=A+B |
3 | -= | Subtract AND assignment | Subtracts right value from the left and assigns the result to left value | A-=B | A=A-B |
4 | *= | Multiply AND assignment | Multiplies right value with the left and assigns the result to left value | A*=B | A=A*B |
5 | /= | Divide AND assignment operator | Divides right value by the left and assigns the result to left value | A/=B | A=A/B |
6 | %= | Modulus AND assignment | Takes the remainder after dividing right value by the left and assigns the result to left value | A%=B | A=A%B |
7 | <<= | Left shift AND assignment | TBD | A<<=B | A=A<<B |
8 | >>= | Right shift AND assignment | TBD | A>>=B | A=A>>B |
9 | &= | Bitwise AND and assignment | TBD | A&=B | A=A&B |
10 | ^= | bitwise exclusive OR and assignment | TBD | A^=B | A=A^B |
11 | |= | bitwise inclusive OR AND assignment | TBD | A|=B | A=A|B |
Bitwise Operators
# | p | q | p&q (AND) | p|q (OR) | p^q (XOR) | ~q (Complement) |
---|---|---|---|---|---|---|
1 | 0 | 0 | 0 | 0 | 0 | 1 |
2 | 0 | 1 | 0 | 1 | 1 | 0 |
3 | 1 | 1 | 1 | 1 | 0 | 0 |
4 | 1 | 0 | 0 | 1 | 1 | 1 |
Example:
# | p | q | p&q | p|q | p^q | ~q |
---|---|---|---|---|---|---|
1 | 00110011 (3 in binary) | 00100111 (27 in binary) | 00100011 = 35 (dec) | 00110111 = 55 (dec) | 00010100 = 20 (dec) | 11001100 = 204 (dec) |