Introduction to C# operators
C# operators are the symbols that are used for different operations like mathematical expressions, function calls, etc.
There are different kinds of operators.
- Assignment operators
- Arithmetic operators
- Conditional operators
- Unary operators
- Increment & Decrement operators
- Relational operators
- Logical operators
Assignment operator
- “=”: This is called the assignment operator. “=” assigns the variable on the left of it with the value on the right of it. In other words, it provides the data on its R.H.S. to the variable in its L.H.S.
Example. X=9;
Here, the operator”=” assigns value 9 to x.
Arithmetic operator
The arithmetic operator is used for mathematical operations. There are 5 types of arithmetic operators.
- +: This operator is called an additional operator. It is used for the addition of two pieces of data.
Example. z=x+y;
Here z will result in the addition of two variables x & y. - -: This operator is called the subtraction operator. It is used for the difference of two pieces of data.
Example. z=x-y;
Here z will result in a difference of two variables x & y. - *: This operator is called the multiplication operator. It is used for product of two data.
Example. z=x*y;
Here z will result in the product of two variables x & y. - /: This operator is called division operator. It is used for the division of two data.
Example. z=x/y;
Here z will result in a quotient for the division of two variables x & y. - %: This operator is called the modulus operator. It is used for finding the remainder after dividing one data from another.
Example. z=x%y;
Here z will result in the remainder after the division of two variables x & y.
Conditional operator
Conditional operators are the operators used to check a condition. The types of conditional operators are
- “ ?": This is called a ternary operator. This is used instead of the if else statement. It avoids long writing of code for if-else statements if the if-else statement is of one statement only like
Then instead of writing this much longer code, we can writeif (something) { Dosomething; } else { Dosomething_else; }
Result = x?y:z;
This means that if the input is x then results in y else results in z. - “ &&”: This is also called logical AND. If the first operand is false, then C# does not evaluate the second operand. It will operate only when both the operands are true.
Example. X&&y
Here, it is checked whether both the operands are true or not. - “||”: This is also called logical OR. If the first operand is true, then C# does not evaluate the second operand. It will operate if any of the operands is true.
Example X||y
Here, it is checked whether any of the operands are true or not. - “??“: This is also called the Null coalescing operator. This returns the value on its left if it is non-null; otherwise, returns the value on the right of it. Example: X??y Here, this returns x if it is non-null; otherwise, returns y.

sathishkumar sathishkumarPosted Mar 9, 2016, 5:40 AM
Good job
Shailesh UkePosted Feb 23, 2016, 4:47 AM
Nice...