Anoop K. C.
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#.
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
Int a = 10;
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 )
9 % 2 = 1
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
Conditional operators are used to perform operations based on simultaneous
evaluation of multiple expressions
&& - and
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
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
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.
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.
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)
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
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.
We can change the order imposed by operator precedence and associativity by
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
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)
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
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Ph: + 91 – 495 40 25 550
Hilite Business Park,
Start up Village