After learning about variables and their data types, lets use them with operators, and see how many types of operators are there in C sharp in this tutorial
Operators are the part of an expression in c sharp. Expressions are usually formed using operands and the operation that have to be performed on them using operators. Operator may be a keyword or a special symbol defining some operation. operators may work on 1 or more than 1 operands, depending on the operation to be performed.
Operators can be categorized into many groups, depending on their operations as follows :
- Assignment operator: It is one of the simplest and widely used operator in c sharp, it is used to assign the legal value to the variables that are declared. It uses the equality symbol i.e “=”. In this expression the left operand is assigned to the right operand, and right operand has to be a variable. and right operand may be variable, immediate value or a sub expression. Example : uint roll_no = 1.
- Arithmetic Operators: It contains operators that are used for doing arithmetic operations like addition, subtraction, multiplication and division using symbols like + , – ,* , / respectively. It works on 2 operands and the resultant answer is stored using assignment operator. C# doesn’t produces fractional answers. Another operator known as modulo i.e % is used for returning remainder from division and not the quotient. Example: int a = 10; int b = a/2; //b=5 int b = a%2 //b=2 int b = a*2 //b=20
- Increment /Decrement Operators : It contains two operators that adds or subtracts 1 from the operand for Incrementing and decrementing respectively and result is stored back in the same operand. It uses ++ and – – keywords, and works with 1 operands. It may be used after the operand and known as postfix operator or before the operand which is known as prefix operator depending on the situation. Example: int a = 10; int b = a++ //b=10 & a=11 int b = ++a //b=11 & a=11 int b = – -a //b= 9 & a = 9 int b = a- – //b=10 & a =9
- Relational Operator : These operators are used in loop and decision making statements for comparing values of its operands. The output after comparison is a boolean value i.e True or False.The operators and their description is given in the following table.
Operator Description == Checks for equality if found equal return true or else return false != Checks for non-equality, if found non equal then return true or else false > Compares value and if left hand side operand is greater then returns true or else false >= Compares value and if left hand side operand is greater or equal then returns true or else false < Compares value and if left hand side operand is less then returns true or else false <= Compares value and if left hand side operand is less or equal then returns true or else false
Example :Int a = 10Int b= 20a<b //truea>b //false
- Logical Operators : This operators are used for performing logical operations like AND, OR, NOT. The operators used are &&, ||. ! respectively. They are usually used when there are two conditions that need to be checked. Example of which will be given later post of decision making statements.