Operators in C#

October9, 2017
by admin

Operators are the symbols that define the operation that is going to take place in an expression.

Operators always operate on operands. Operand may be a value or a variable or other expression. The operand can be on both sides of the operator or one side of the operator.

Let’s look some commonly used operators in C#.

Arithmetic operators:

These are basically used to do arithmetic operations such additions, subtractions etc.

1. Additional operator (+):

This is used for addition. It is a plus symbol.

Ex:

5 + 4 = 10

int a = 5;
int b = 4;
int c = a + b;

2. Subtraction operator (-):

This is used for subtraction. It is a minus symbol.

Ex:

10 – 5 = 4;

int c = 10; 
int a = 5;
int b = c - a;

3. Multiplication operator (*);

This is used for multiplication. It is an esoteric symbol.

Ex:

10 * 2 = 20;

int a = 10;
int b = 2;
int c = a * b;

4. Divisional operator (/);

This is used for division. It is a forward slash symbol.

Ex:

10 / 2 = 5;

int c = 10;
int b = 2;
int a = c / b;

5. Modulus operator(%):

This is used in an expression to return remainder. It is a percentage symbol.

Ex:

10 % 3 = 1;

int a = 10;
int b = 3;
int c = a % b;

6. Increment operator (++):

This is used in an expression to increase value of an integer variable by 1.

Ex:

int a = 5;
a++;

Here the expression will return 6 by incrementing the value of ‘a’ by 1.

7. Decrement operator (–):

This is used in an expression to decrease the value of an integer variable by 1.

Ex:

int a = 5;
a--;

Here the expression will return 4 by decreasing the value of ‘a’ by 1.


Assignment operators:

These are used in an expression to assign a value or a result of an expression to another variable.

1. Assignment operator (=):

This is used to assign the value from right hand operator or an expression to the left hand side variable.

Ex:

int x = 5;
int y;
y = x; 

Here the value of ‘x’ is assigned to variable y.

2. Increment assignment (+=):

Ex:

x += y;

This is same as x = x + y.

Here ‘y’ value is added to ‘x’ value and then assign result to ‘x’.

3. Decrement assignment (-=):

Ex:

x -= y;

This is same as x = x – y;

Here ‘y’ value is subtracted from ‘x’ value and then assign the result to ‘x’.

4. Multiplication assignment (*=):

Ex:

x *= y;

This is same as x = x * y;

Here ‘x’ value is multiplied with ‘y’ value and then assign the result to ‘x’.

5. Division assignment (/=):

Ex:

x /= y;

This is same as x = x / y;

Here ‘x’ value is divided by ‘y’ value and then assign the result to ‘x’.

6. Modulus assignment (%=):

Ex:

x %= y;

This is same as x = x % y;

Here remainder is calculated by dividing ‘x’ value by ‘y’ value and then assign the result to ‘x’.


Conditional (Logical) operators:

These operators are used in conditional expression where the result of an expression is either true or false.

1. Conditional (logical) AND operator (&&):

With conditional ‘AND’ operator an expression returns true if the expressions on both sides of operator return true. This is a double ampersand symbol.

Ex:

expression 1 && expression 2

In the above expression, if both expression 1 and expression 2 returns true then the total expression return true. If any one of the expression returns false, then the total expression returns false.

true && false will return false.

false && true will return false.

false && false will return false.

true && true will return true.

2. Conditional (logical) OR operator (||):

With conditional ‘OR’ operator the expression return true if either of the expressions returns true. This is a double pipe symbol.

Ex:

expression 1 || expression 2

In the above expression if either expression 1 or expression 2 returns true then total expression will return true. Expression return false only when both the expression 1 and expression 2 returns false.

true || false will return true.

false || true will return true.

true || true will return true.

false || false will return false.

3. Logical NOT operator (!):

This operator is used change the result of the expression. If an expression returns true, then result will be false. If an expression returns false, then result will be true.

Ex:

!(true && true)

will return false.

!(true != false)

will return true

!(expression 1 && expression 2)

will return false if both expression returns true and total expression returns true.

will return true if the total expression returns false.

4. Conditional operator (? 🙂:

This is also known as the turnary operator. Here 2 symbols are used, question mark and colon with three expressions.

Ex:

expression 1 ? expression 2 : expression 3;

After first expression, the question mark symbol is used and colon symbol is used between expression second and expression third.

Here result of expression 1 decides which expression (expression 2 or expression 3 ) will execute. If expression 1 returns true, then expression 2 will be executed and if expression 1 returns false then expression 3 will be executed.


Equality operators:

These operators are used to check the equality of operands.

1. Is equal operator (==):

This is used to check if both operands are equal. This is a double equal symbol.

Ex:

x == y

Here the expression will return true if x and y values are same.

2. Not equal operator (!=):

This is used to check if one operand is not equal to another. This is an exclaimation equal symbol.

Ex:

x != y;

Here expression will return true if x and y values are different.


Relational operators:

These operators are used to check the relational behavior between the operands (expressions) results.

1. Less than operator (<):

Ex:

x < y;

This expression returns true if 'x' value is less than 'y' value.

2. Greater than operator (>):

Ex:

x > y;

This expression returns true if 'x' value is greater than 'y' value.

3. Less than equal to operator (<=):

Ex:

x <= y;

This expression returns true if 'x' value is greater than or equal to 'y' value.

4. Greater than or equal to operator (>=):

Ex:

x >=y;

This expression returns true if 'x' value is greater than or equal to 'y' value.


Other operators:

1. Is operator (Is):

This is a type compatibility operator.

An expression returns true if the type of left operand can be cast to the type of right operand.

Note that the right operand is of static type.

This is a 'Is' keyword.

Ex:

x Is ;

Here expression returns true if type of 'x' can be cast to the type specified in the right hand operand.

2. As operator (As):

This is a type conversion operator.

An expression returns the result by casting the left operand by the type specified in the right operand.

Note that the right operand is of static type.

This is a 'As' keyword.

Ex:

x As ;

Here expression returns 'x' by casting it to the left operand type.

An expression returns null if casting (T)x throws an exception.


Operator precedence in C#:

Every operator in C# has a certain precedence.

One group of operators in C# has differing precedence that other group of operators.

For ex: *, / and % operators have higher precedence than +, - operators.

Precedence plays an important role when there is more than one operator in an expression.

Higher precedence operators are evaluated first than lower precedence operators in an expression.

Ex:

10 + 5 * 2;

This will return 20 and not 30 because since the multiplication operator has higher precedence over addition operator 5 will be multiplied to 2 first and return 10 and then 10 will be added to 10 and the result will be 20.

Programmer note:

It may not be always possible to remember and recall the precedence of operators. So to avoid confusion and errors it is better to use grouping with opening and closing curly braces for the sub expression that you want to evaluate first.

Ex:

(10 + 5) * 2;

This will return 30 since we have grouped the expression 10 + 5 so it will evaluate first then result 15 will be multiplied to 2 and return 30.

C# Fundamentals