Operators are elements in C# that are applied to operands in expressions or statements. Unary operators take one operand, like increment (++), while binary operators take two operands, such as arithmetic operators (+, -, *, /). The conditional operator (?:) is the sole ternary operator, taking three operands. Some common operators are assignment (=), arithmetic, comparison, conditional (&&, ||), ternary (?:), and null coalescing (??). Operator precedence and associativity determine the order of evaluation in expressions with multiple operators. Parentheses can be used to override precedence.
3. In C#, an operator is a program element that is applied to one or more operands in
an expression or statement.
Operators that take one operand, such as the increment operator (++)
or new, are referred to as unary operators.
Operators that take two operands, such as arithmetic operators (+,-
,*,/), are referred to as binary operators.
One operator, the conditional operator (?:), takes three operands and is the
sole ternary operator in C#.
5. Assignment operator
Assignment operator is used to assign a value to a variable, that is it assigns the
value on the right hand side to the variable on the left hand side
Example:
Int a = 10;
6. Arithmetic operators
Arithmetic operators are used to perform arithmetic operations
+ - Add
- - Subtract
* - Multiply
/ - Division
% - Modulus (divides the value on the left side by the value
on the right side and outputs the remainder )
Example:
9 % 2 = 1
7. Comparison operators
Comparison operators are used to compare the expression on the left hand side
(LHS) with the expression on the right hand side (RHS) and it outputs a boolean
value as the result
== - equal to
!= - not equal to
> - Greater than
>= - Greater than or equal to
< - Less than
<= - Less than or equal to
8. Conditional operators
Conditional operators are used to perform operations based on simultaneous
evaluation of multiple expressions
&& - and
Example:
If (a == b && b != c)
[if a equal to b and b not equal to c – this condition will
evaluate to true if and only if (a == b) is true and (b !=
c) is true]
|| - or
Example:
If (a == b || b != c)
[if a equal to b or b not equal to c – this condition will
evaluate to true if either of (a == b) is true or (b != c) is
true]
9. Ternary Operator (?:)
Ternary Operator is used to Return one value if the condition is true, or another
value if it is false. It is some sort of if-else code in a short form.
Example:
10. Null Coalescing operator (??)
A null coalescing operator returns the left-hand operand if the operand is not null;
otherwise it returns the right hand operand.
Example:
x ?? y , means if x is non-null, evaluate to x; otherwise, y.
In the syntax x ?? Y
x is the first operand which is a variable of a nullable type.
y is the second operand which has a non-nullable value of the same type.
While executing code, if x evaluates to null, y is returned as the value
The null coalescing operator (??) is right-associative which means it is evaluated
from right to left. So if you have an expression of the form x ?? y ?? z, this
expression is evaluated as x?? (y?? z)
11. Evaluation and operator precedence
In an expression that contains multiple operators, the order in which the operators
are applied is determined by operator precedence, associativity, and parentheses.
Each operator has a defined precedence. In an expression that contains multiple
operators that have different precedence levels, the precedence of the operators
determines the order in which the operators are evaluated.
For example, the following statement assigns 3 to n1.
n1 = 11 - 2 * 4;
The multiplication is executed first because multiplication takes precedence over
subtraction.
12. Associativity
When two or more operators that have the same precedence are present in an
expression, they are evaluated based on associativity.
Left-associative operators are evaluated in order from left to right. For example,
x * y / z is evaluated as (x * y) / z.
Right-associative operators are evaluated in order from right to left.
The assignment operator is right associative. For exmple:
The assignment operators and the ternary operator (?:) are right associative. All
other binary operators are left associative.
13. Adding paranthesis
We can change the order imposed by operator precedence and associativity by
using parentheses.
For example,
2 + 3 * 2 ordinarily evaluates to 8, because multiplicative operators take
precedence over additive operators.
However, if we write the expression as (2 + 3) * 2, the addition is evaluated before
the multiplication, and the result is 10
14. Operator overloading
We can change the behavior of operators for custom classes and structs. This
process is referred to as operator overloading
To overload an operator on a custom class requires creating a method on the class
with the correct signature. The method must be named "operator X" where X is
the name or symbol of the operator being overloaded.
Unary operators have one parameter, and binary operators have two parameters.
In each case, one parameter must be the same type as the class or struct that
declares the operator, as demonstrated in the following example:
public static Complex operator + (Complex c1, Complex c2)
15. Follow us @ twitter.com/baabtra
Like us @ facebook.com/baabtra
Subscribe to us @ youtube.com/baabtra
Become a follower @ slideshare.net/BaabtraMentoringPartner
Connect to us @ in.linkedin.com/in/baabtra
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
16. Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Cafit Square,
Hilite Business Park,
Near Pantheerankavu,
Kozhikode
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com