Operators are something that used to perform operations(mathematical or logical) based on the type of operator used
Also, operators tells you about the relationship between the operands(in case of two or more).
Types:
1. Arithmetic Operators: They are used to perform the arithmetic operations such as addition, subtraction, multiplication, division, etc.
· Addition Operator (+)
Performs addition operation
Ex: 9+3=12
· Subtraction Operator (-)
Performs subtraction operation
Ex: 9-3=6
· Multiplication Operator (*)
Performs multiplication operation
Ex: 9*3=27
· Division Operator (/)
Performs division operations and gives quotient as the output.
Ex: 9/3=3
· Modulo Operator (%)
Performs division operation and gives remainder as the output.
Ex: 9%3=0
· Increment Operator (++)
Increases variable value by 1
Ex: A = 10
A++ = 11
(a) Pre Increment: It increments the value before we use it in our expression.
Ex: int a = 2;
int b = ++a; // b = 3
(b) Post Increment: It increments the value after executing the expression.
Ex: int a = 2;
int b = a++; //b = 2
· Decrement Operator (--)
Decreases variable value by 1
Ex: A = 10
A-- = 9
(a) Pre Decrement : It decrements the value before we use it in our expression.
Ex: int a = 2;
int b = --a; // b = 1
(b) Post Decrement: It decrements the value after executing the expression.
Ex: int a = 2;
int b = a--; //b = 2
Precedence of Arithmetic Operators
Ø Postfix/Prefix: ++, --
Ø Multiplicative: *, /, %
Ø Additive: +, -
2. Relational Operators: They are used to determine the relation between the operands.
· Equality Comparison (==)
It checks whether the two operands are equal or not. Results true if they are equal otherwise, false.
Ex: A == B is false
A == A is true
· Inequality Operator (!=)
It checks whether the two operands are equal or not. Results true if they are not equal otherwise, false.
Ex: A!=B is true
A!=A is false
· Greater Than (>)
It checks whether the first operand is greater than the second operand. Results true if condition is satisfied otherwise, false.
Ex: 6>5 will return true
5>6 will return false
· Less Than (<)
It checks whether the first operand is smaller than the second operand. Results true if smaller otherwise, false.
Ex: 5<6 will return true
6<5 will return false
· Greater Than Equal To (>=)
It checks whether the first operand is greater than or equal to the second operand. Results true if condition is satisfied otherwise, false.
Ex: 1>=1 will return true
2>=1 will return false
· Less Than Equal To (<=)
It checks whether the first operand is smaller than or equal to the second operand. Results true if condition is satisfied otherwise, false.
Ex: 1<=1 will also return true
2<=1 will return false
3. Logical Operators: They are used to perform logical operations between the operands and returns boolean value(true or false)
· Logical AND Operator (&&)
This operator returns true value if both the conditions(statements) are true.
Ex: 1<5 && 1<10 will return true
1<5 && 11<10 will return false
11<10 && 1<5 will also return false
· Logical OR Operator (||)
This operator returns true if one of the conditions(statements) is true.
Ex: 1<5 || 5>6 will return true
1<5 || 5<6 will return false
· Logical Not (!)
This operator reverses the result, means it returns true if the condition is false.
Ex: !(1<5) will return false
!(1>5) will return true
4. Bitwise Operators: They are used to perform bit manipulation operations. It uses binary representation of both operands for the operation.
· Bitwise AND (&)
It takes 2 operands as input and performs AND operation on every bit of those 2 operands. Its result is 1 if both bits are 1.
Ex: a=0, b=1; a & b = 0
a=1, b=1; a & b = 1
· Bitwise OR (|)
It takes 2 operands as input and performs OR operation on every bit of those 2 operands. Its result is 1 if any one of the two bits is 1.
Ex: a=0, b=1; a | b = 1
a=0, b=0; a | b = 0
· Bitwise XOR (^)
It takes 2 operands as input and performs XOR operation on every bit of those 2 operands. Its result is 1 if the two bits are different.
Ex: a=0, b=1; a ^ b = 1
a=1, b=1; a ^ b = 0
a=0, b=0; a ^ b = 0
· Bitwise Left Shift (<<)
It takes 2 operands as input and performs left shifting of bits on those two operands and determines the no. of places to shift.
Ex: a= 2=0100; a<<1 = 1000 = 8
· Bitwise Right Shift (>>)
It takes 2 operands as input and performs right shifting of bits on those two operands and determines the no. of places to shift.
Ex: a = 4 = 0100; a>>1 = 0010 = 2
· Bitwise Complement Operator (~)
It inverts the binary bits of the operand means 1 to 0 and 0 to 1.
Ex: a= 4 = 0100; ~a = 1011 = 11
5. Assignment Operators: They are used to assign value to a variable. The left side operand of the assignment operator is the variable and the operand on its right is the value that should be of the same data type of the variable.
· Assignment Operator (=)
It assigns value from right side operands to its left side operand.
Ex: int a; a = 5
· Addition Assignment Operator (+=)
It performs addition operation and assigns the result to the variable.
Ex: a += 5 is equals a = a + 5
· Subtraction AND Assignment Operator (-=)
It performs subtraction operation and assigns the result to the variable.
Ex: a -= 5 is equals a = a - 5
· Multiplication Assignment Operator (*=)
It performs multiplication operation and assigns the result to the variable.
Ex: a *= 5 is equals a = a * 5
· Division Assignment Operator (/=)
It performs division operation and assigns the result to the variable.
Ex: a /= 5 is equals a = a / 5
· Modulus Assignment Operator (*=)
It performs division operation and assigns the remainder of division to the variable.
Ex: a %= 5 is equals a = a % 5
· Left Shift Assignment Operator (<<=)
It shifts the specified no. of bits to the left and assigns the result to the variable.
Ex: a <<= 2 is equals a = a << 2
· Right Shift Assignment Operator (>>=)
It shifts the specified no. of bits to the right and assigns the result to the variable.
Ex: a >>= 2 is equals a = a >> 2
· Bitwise AND Assignment Operator (&=)
It performs bitwise AND operation and assigns the result to the variable.
Ex: a &= 2 is equals a = a & 2
· Bitwise Exclusive OR Assignment Operator(^=)
It performs bitwise XOR operation and assigns the result to the variable.
Ex: a ^= 2 is equals a = a ^ 2
· Bitwise Inclusive OR Assignment Operator(|=)
It performs bitwise OR operation and assigns the result to the variable.
Ex: a |= 2 is equals a = a | 2
6. Miscellaneous Operators
· sizeof()
It returns the size of the data type.
Ex: sizeof(int) will return 4
· typeof()
It returns the type of the class means it gives the System.Type object for a type.
Ex: typeof(double) will return System.Double
· Address Specifier (&)
It returns the address of a variable.
Ex: &a will return the actual address of variable a.
· Reference Operator (*)
It is used to create a pointer to a variable.
Ex: *b is a pointer to a variable.
· Conditional Operator (?:)
It is a ternary operator. If the condition stated by the expression is true, the result will be equal to Expression1 and if it is false, result will be equal to Expression2.
Syntax: Condition ? Expression1 : Expression2
Ex: (3>5) ? Yes : No will result No
(3<5) ? Yes : No will result Yes
Type Conversion in C#
It is a process of converting one predefined data type to another data type.
Following are the types of conversion in C#
1. Implicit Type Conversion: It’s conversion that is performed by the compiler without any intervention of the user. It is a conversion from smaller to larger size type.
char->int->long->float->double
Ex: int a=9;
double d = a;
2. Explicit Type Conversion: It’s an explicit conversion of an operand to a specific type by the user. It is a conversion from larger type to smaller size type.
double ->float->long->int->char
Ex: double d = 9.78;
Int n = (int)d;
Data Types in C#
They are means to identify the type of data and associated operations of handling it.
· int
It stores whole numbers without decimals. It is of 4 bytes.
Ex: int number = 5
· long
It also stores whole numbers. This data type is used when int data type is not large enough to store values. And you have to end the value of long data type with ‘L’. It is of 8 bytes.
Ex: long number = 16000000000L
· float
It stores the fractional numbers and you have to end the value with ‘F’. It can store 6 to 7 decimal digits. It is of 4 bytes.
Ex: float number = 2.32F
· double
It also stores fractional numbers and you have to end the value with ‘D’. It is of 8 bytes and can store up to 15 decimal digits.
Ex: double number = 13.22D
· bool
It can only store value true or false. It is of 1 bit.
Ex: bool isMonday = true;
· char
It is used to store a single character, enclosed within single quotes like ‘A’. It is of 2 bytes.
Ex: char Grade = ‘A’
· string
It is used to store sequence of characters, enclosed within double quotes like “Hello”. It is of size 2 bytes per character.
Ex: string day = “Monday”