C# Operators
C# Operators are a symbol that tells the computer to perform certain mathematical or logical manipulations. It is used to manipulate data and variables in programs. C#, supports a rich set of operators, there are 8 types of C# Operators given below:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Increment and decrement Operators
5. Assignment Operators
6. Conditional Operators
7. Bitwise Operators
8. Special Operators
Arithmetic Operators:
C# provides all basic arithmetic operators. These operators can operate on any built-in numeric data type. We can’t use these operators on the Boolean type. They are listed in the table:
Operator | Meaning |
+ | Addition or unary plus |
- | Subtraction or unary minus |
* | Multiplication |
/ | Division |
% | Modulo division |
Relational Operators:
When we often compare two quantities and depending on their relation make certain decisions that are called Relational Operators. C# supports six Relational Operators which are listed in the table:
Operator | Description |
< | is less than |
<= | is less than or equal to |
> | is greater than |
>= | is greater than or equal to |
(==) | is equal to |
!= | is not equal to |
Logical Operators:
The Logical Operators && and || are used when we want to form compound conditions by combining two or more relations. C# supports six Logical Operators which are listed in the table:
Operator | Description |
&& | logical AND |
|| | logical OR |
! | logical NOT |
& | bitwise logical AND |
| | bitwise logical OR |
^ | bitwise logical exclusive OR |
Increment and decrement Operators:
In C#, there are two useful operators are used and that is Increment and decrement Operators. The increment operator (++) is used to increase the value of an integer by 1. The decrement operator (- -) is used to reduce the value of an integer by 1. It has the following form:
x = 5; x++ or ++x will produce the result x = 6 y = 5; y- – or – -y will produce the result y = 4
Assignment Operators:
Assignment Operators are used to assign the value of an expression to a variable. It has the following syntax:
v op=exp
where v is a variable, exp is an expression, op is a C# binary operator. The operator op is also known as Shorthand Assignment Operator. It has the following syntax:
v op=exp It is equivalent to v=v op(exp);
Example:
x+=y+1; It is equivalent to x=x+(y+1);
Conditional Operators:
The character pair ?: is a ternary operator. This operator is used to construct conditional expressions, So it is called Conditional Operator. It has the following Syntax:
exp1 ? exp2 : exp3
where exp1,exp2,exp3 are expressions. The operator ?: works as follows:
exp1 is evaluated first, it is true. then exp2 is evaluated and becomes the value of the conditional expression. If exp1 is false, exp3 is evaluated and its value becomes the value of the conditional expression.
Example:
x=5; y=6; p=(x>y)?x:y;
Bitwise Operators:
In C#, Bitwise Operators are used to manipulate data at the bit level. These operators may be used for testing the bits or shifting them to the right or left. Bitwise operators may not be applied to floating-point data types.
Special Operators:
In C#, there are 8 types of Special Operators are exists given below:
1. is –
2. as
3. typeof (type operator)
4. sizeof (size operator)
5. new
6. .(dot)
7. checked
8. unchecked