Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

An operator is a basic code element that performs some operation on

one or more values to createa result. The values the operator acts upon
are operands. For example, in the following statement,the operator is +
(addition), the operands are B and C, and the result is assigned to the
variable A.

A=B+C
C# operators :
1. Arithmetic
2. concatenation,
3. comparison,
4. logical,
5. Bitwise,
6. Conditional ,
7. And Assignment
Arithmetic Operators
Concatenation Operator
• The + symbol represents both numeric addition
and string concatenation. The following code
shows an example of string concatenation.
string firstname = "Rod";
string lastname = "Stephens";
string name = firstname + ' ' + lastname;
Comparison Operators
Comparison operators compare one value to another
and return a boolean value (true or false), depending
on the result.
Logical Operators
Comparison operators compare one value to another
and return a boolean value (true or false), depending
on the result.
Bitwise Operators
 Bitwise operators work much like logical operators do, except they
compare integer values one bit at a time.

 The bitwise negation operator ~ flips the bits in its operand from 1 to
0 and vice versa. The following shows an example:
~10110111
= 01001000

 The bitwise And operator & places a 1 in a result bit if both of its
operands have a 1 in that position. The following shows an example:
10101010
& 00110110
= 00100010
 The bitwise Or operator | places a 1 in the result if either of its
operands has a 1 in the corresponding position. The following
shows an example:
10101010
| 00110110
= 10111110

 The bitwise Xor operator ^ places a 1 bit in the result if exactly


one of its operands, but not both, has a 1 in the corresponding
position. The following shows an example:
10101010
^ 00110110
= 10011100
There are no bitwise equivalents for the && and || operators.
Conditional Operators
The conditional operator ?:, which is sometimes called the
ternary operator, takes three operands. The first operand is a
boolean value. If that value is true, the operator returns its
second operand. If the first operand is false, the operator
returns its third operand.

int foperand = 10;

string soperand=“Hello”, toperand=“World”, ans;

ans = (foperand = = 10) ? soperand : toperand;


Assignment Operators
To make this sort of operation easier, C# provides a set of
assignment operators. These consist of a normal operator
followed by =. For example, the following statement shows
the preceding code rewritten to use the += assignment
operator.
x += 10;
This operator basically means, “Set x equal to its current
value plus 10.”

The complete list of assignment operators is: =, +=, -=, *=,


/=, %=, &=, |=, ^=, <<=, and >>=.
Operator Precedence
 Precedence determines which operator C# executes first.
StringBuilder
The + operator is useful for concatenating a few strings
together, but if you must combine a large number of strings,
you may get better performance by using the StringBuilder
class. This class is optimized for performing long sequences
of concatenations to build big strings

Strings are immutable objects

Stringbuilders are mutable or dynamically change able


without creating new object.
DateTime Operator
Operator Overloading
• Two forms of operator methods:
– unary operators
– binary operators
• General form for unary operator
public static ret-type operator op(prarm-type perand) {//oprations}
• General form for binary operator
public static ret-type operator op(prarm-type1 operand1,
prarm-type2 operand2){ //oprations}
Operator Overloading
Overloadable operators:
• +-*/%
• ++ -- (both postfix and prefix)
• == != < > <= >=
• &|~^!
• true false
Operators Restrictions
• = cannot be overloaded
• && and || cannot be overloaded directly
– are evaluated using &, |, true, false
• *=, /=, +=, -=, %= cannot be overloaded
– are evaluated using *, /, +, -, % operators
• &=, |=, ^=, >>=, <<= cannot be overloaded
• Relational operators must be paired (< and >, <= and
• >=, == and !=)
• Override the Equals method if overloading == and !=
• Override the GetHashCode method if overriding
Equals method

You might also like