**Operators in C# are Special Symbols to Denote the Operation That the Program Needs to Perform. This Tutorial Explains C# Operators in Detail With Examples:**

In our previous tutorial, we learned about Conditional Statements in C#. We also learned how to use if, if-else and if-else if statements to define different conditions.

A conditional statement such as “if” is also known as a decision-making statement as they provide the user with a mechanism to define an outcome based on a decision defined by operators.

The operators offer a way to define decisions based on Logic, Arithmetic Operations, Comparison, etc.

**=> See Our Complete C# Training Series Here**

What You Will Learn:

## C# Operators

Operators in C# are special symbols that denote the operation that the program needs to perform on the operands. **For Example,** they can be used to evaluate a variable or perform an operation on a variable to make a proper expression.

C# offers a wide variety of operators such as Arithmetic operators, Relational operators, Assignment operators, Logical operators, Unary operators, etc. In this tutorial, we will be discussing some of the important operators along with their usage.

### Arithmetic Operators

The arithmetic operator allows the program to perform general algebraic operations against numeric values.

**There are five basic operators present in the C# programming language.**

**Addition (symbol “+”):**Perform the addition of operands.**Subtraction (symbol “-“):**Performs subtraction of operands.**Division (symbol “/”):**Performs division of operands.**Multiplication (symbol “*”):**Performs multiplication on operands.**Modulus (symbol “%”):**Returns reminder after the division of integer.

**Example:**

int a = 10; int b = 5; int result; result = a + b; result = a - b; result = a * b; result = a / b; result = a % b;

- The result of the first operation will be 15, i.e. the summation to two integers.
- The result of the second operation will be 5, i.e. subtraction of two integers.
- The result of the third operation will be 50, i.e. multiplication between two integers.
- The result of the fourth operation will be 2 i.e. output of division of two integers.
- The result of the fifth operation will be 0 as there will be no reminder left when two given integers are divided.

One should remember that the result of the operation will depend upon the data type used to store the result.

So, if the division of two integer values returns a float value and if the result is assigned to an integer variable then the decimal part will be lost due to different data types. To learn more about data types and conversion, please visit our previous tutorial.

The modulus operator is different than the other operators, it returns the value of the reminder from the division of integers. Let’s say if we divide 20 by 6, then the division operator will return an answer as 3 (the quotient) and the modulus operator will return 2 i.e. the reminder of the division.

Other than the above 5 defined operators, C# also offers two special operators that increase or decrease the value of a variable by 1.

**These are:**

**Increment operator**: Denoted by the symbol “++”**Decrement operator**: Denoted by the symbol “- -“

These operators can be pre-fixed or suffixed with variables for operation.

**Example:**

int a = 10; int b = 5; int increment; int decrement; increment = a++; decrement = b--;

In the above example, the answer for increment will be 11 i.e. the value of a will be increased by 1. While the answer for decrement will be 4 i.e. the value of b will be decreased by 1.

### Relational Operators

Any relation between the two operands is validated by using relational operators. Relational operators return Boolean values. If the relation between two operands is successfully validated then it will return “true” and if the validation fails then “false” will be returned.

Relational operators are mainly used in decision making or for defining conditions for loops.

**Let’s have a look at the Relational Operators offered by C#:**

**Greater than operator:**(denoted by “>”): Validates greater than the relation between operands.**Less than operator:**(denoted by “<“): Validates less than the relation between operands.**Equals To operator:**(denoted by “==”): Validates the equality of two operands.**Greater than or equals to**(denoted by “>=”): Validates greater than or equals to the relation between the two operands.**Less than or equals to**(denoted by “<=”): Validates less than or equals to the relations between the two operands.**Not equal:**(denoted by “!=”): Validates not an equal relationship between the two operands.

int a = 10; int b = 5; bool validate; validate = a > b; //1 Console.WriteLine(validate); validate = a < b; //2 Console.WriteLine(validate); validate = a == b; //3 Console.WriteLine(validate); validate = a >= b; //4 Console.WriteLine(validate); validate = a <= b; //5 Console.WriteLine(validate); validate = a != b; //6 Console.WriteLine(validate);

**The output of the above program will be:**

- a>b will return “True”.
- a<b will return “False”.
- a==b will return “False”.
- a>=b will return “True” as a is greater than b and the operator is looking for a successful evaluation of any of the given conditions for returning a true value. As the given example returns “True” in both cases, the operator will return true.
- a<=b will return “False” as a is neither less than b nor equal to b.
- a!=b will return “True” as a does not equal b.

### Assignment Operators

Assignment operators are used for assigning value to a variable. These are generally used before an arithmetic operator.

**Let’s have a look at the Assignment Operators offered by C#:**

**(i) Equals to (“=”): **It is one of the simplest assignment operators. It assigns the value of one operand to another. i.e. the value of the right side operand to the left side operand.

**Examp****le:** a = b

**(ii) Add Equal to the Assignment Operator: **As the name suggests it is a combination of plus “+” and equal to “=”. It is written as “+=” and it adds the operand at the right side to the left operand and stores the final value in the left operand.

**Example:** a +=b means (a = a+b)

**(iii) Subtract Equal Assignment Operator: **Similar to the add equals, it subtracts the value of the right operand from the left operand and then assigns the value to the left operand.

**Example:** a -=b means (a = a-b)

**(iv) Division Equal to the Assignment Operator: **It divides the value of the right operand with the left operand and then stores the result in the left operand.

**Example:** a /= b mean (a= a/b)

**(v) Multiply Equal to the Assignment Operator: **It multiplies the value of the right operand with the left operand and then stores the result in the left operand.

**Example:** a *= b mean (a= a*b)

**(vi) Modulus Equals to the Assignment Operator: **It finds the modulus of the left and right operand and stores the value in the left operand.

**Example:**

a %=b means (a= a%b)

**Given below is a program to have more clarity:**

int a = 10; int b = 5; a += b; //1 Console.WriteLine(a); a -= b; //2 Console.WriteLine(a); a /= b; //3 Console.WriteLine(a); a *= b; //4 Console.WriteLine(a); a %= b; //5 Console.WriteLine(a);

**Output**

- The first value will return 15 i.e. a = a + b.
- The second operator will return 10 i.e. a = a-b.
- The third operator will return 2 i.e. a = a/b.
- The fourth operator will return 50 i.e. a = a*b.
- The fifth operator will return 0 i.e. a= a%b.

### Logical Operators

Logical operators are used for performing logical operations. Logical operators work with Boolean expressions and return a Boolean value. Logical operators are used with the conditional operators in loops and decision-making statements.

**Logical Operators and their usage.**

#### #1) Logical AND Operator

**Symbol:** “&&”

AND operator returns true when both the values are true. If any of the value is false then it will return false.

** For example, **A && B will return true if both A and B are true, if any or both of them are false then it will return false.

#### #2) Logical OR Operator

**Symbol:** “||”

OR operator returns true if any of the condition/operands is true. It will return false when both of the operands are false.

**For example,** A || B returns true if the value of either of A or B is true. It will return false if both A and B have false values.

#### #3) Logical NOT Operator

**Symbol:** “!”

NOT operator is used to reverse the logical conclusion of any condition. If the condition is true then it will return false and if the condition is false then it will return true.

**Example,** !(A||B) returns false if “A||B” returns true and will return true if “A||B” returns false.

**Example Program:**

int a = 10; int b = 5; bool result; // AND operator result = (a == b) && (a>b) Console.WriteLine(result); //OR Operator result = (a == b) || (a>b) Console.WriteLine(result); //NOT Operator result = !((a == b) || (a>b)) Console.WriteLine(result);

**The output of the above program will be:**

- The first value will return false as one of the conditions i.e. a==b is false.
- The second operator will return true as one of the conditions i.e. a > b is true.
- The third operator will return false i.e. the negation of the result of OR operator.

## Conclusion

In this tutorial, we learned about the different types of operators in the C# programming language. We learned about the usage and symbols of these operators. The arithmetic operator is used by the program to perform simple algebraic operations like addition, subtraction, multiplication, division, etc.

Relational operators are the ones that are used to validate a relationship between the two operands as if they are equal, greater than, less than, etc. Assignment operators are used for assigning values to a variable. A most simple example of the assignment operator is “equal to”. Logical operators are used for performing logical operations like AND, OR, NOT, etc.

Operators are extensively used for declaring conditions in decision-making statements, while using loops or while performing algebraic operations.

**=> Check For Our Ultimate C# Training Guide Here**