C# Operators - NetwaxLab

Breaking

Facebook Popup

BANNER 728X90

Tuesday, February 10, 2015

C# Operators

An operator performs the operations on operands. The combination of operands and operators are called expressions. There are lots of operators in C- Sharp language. The operators of an expression indicate which operations to apply to the operands. Operators in programming languages are taken from mathematics. An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. Programmers work with data. The operators are used to process data. An operand is one of the inputs (arguments) of an operator.

C-Sharp provides the following type of operators:

(Note: Assume variable A has 20 and B has 5.)
  • Arithmetic Operators: Arithmetic operators perform addition, subtraction, multiplication, division, and remainder operations. This is all familiar from the mathematics.

Operator
Description
Example
+
Adds two operands
A + B = 25
-
Subtracts two operands
A - B = 15
*
Multiplies both operands
A * B = 100
/
Divides numerator by de-numerator
A / B = 4
%
Modulus Operator and remainder of after an integer division
A % B = 0
++
Increment operator increases integer value by one
A++ = 21
--
Decrement operator decreases integer value by one
A-- = 19

Example: 1
Input
Output
  • Relational Operators: Relational operators perform the conditional operations. These operators return boolean type value true or false.

Operator
Description
Example
==
Checks if the values of two operands are equal or not, if yes then condition becomes true.
(A == B) is not true.
!=
Checks if the values of two operands are equal or not, if values are not equal then condition becomes true.
(A != B) is true.
> 
Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.
(A > B) is true.
< 
Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.
(A < B) is not true.
>=
Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.
(A >= B) is true.
<=
Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.
(A <= B) is not true.

Example: 2
Input
Output
  • Logical Operators: There are three logical operators. The bool keyword is used to declare a Boolean value.

Operator
Description
Example
&&
Called Logical AND operator. If both the operands are nonzero then condition becomes true.
(A && B) is false.
||
Called Logical OR Operator. If any of the two operands is nonzero then condition becomes true.
(A || B) is true.
!
Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.
!(A && B) is true.

The ||, and && operators are short circuit evaluated. Short circuit evaluation means that the second argument is only evaluated if the first argument does not suffice to determine the value of the expression: when the first argument of the logical and evaluates to false, the overall value must be false; and when the first argument of logical or evaluates to true, the overall value must be true. Short circuit evaluation is used mainly to improve performance.

Example: 3
Input
Output
  • Assignment Operators: The assignment operator = assigns a value to a variable. A variable is a placeholder for a value. In an equation, the = operator is an equality operator. The left side of the equation is equal to the right one.

Operator
Description
Example
=
Simple assignment operator, Assigns values from right side operands to left side operand
C = B will assign value of B into C
+=
Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand
B += A is equivalent to B = B + A
-=
Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand
B -= A is equivalent to B = B – A
*=
Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand
B *= A is equivalent to B = B * A
/=
Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand
B /= A is equivalent to B = B / A
%=
Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand
B %= A is equivalent to B = B % A
<<=
Left shift AND assignment operator
B <<= 2 is same as B = B << 2
>>=
Right shift AND assignment operator
B >>= 2 is same as B = B >> 2
&=
Bitwise AND assignment operator
B &= 2 is same as B = B & 2
^=
bitwise exclusive OR and assignment operator
B ^= 2 is same as B = B ^ 2
|=
bitwise inclusive OR and assignment operator
B |= 2 is same as B = B | 2

Example: 4
Input
Output

  • Bitwise Operators: Decimal numbers are natural to humans. Binary numbers are native to computers. Binary, octal, decimal, or hexadecimal symbols are only notations of the same number. Bitwise operators work on bits and perform bit by bit operation. This operator’s work with bits of a binary number. The truth tables for &, |, and ^ are as follows:

P
Q
p & q
p | q
p ^ q
0
0
0
0
0
0
1
0
1
1
1
1
1
1
0
1
0
0
1
1

Assume if A = 60; and B = 13; now in binary format they will be as follows:

A = 0011 1100
B = 0000 1101
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011

Operator
Example
Description
&
Binary AND Operator copies a bit to the result if it exists in both operands.
(A & B) will give 12. which is 0000 1100
|
Binary OR Operator copies a bit if it exists in either operand.
(A | B) will give 61.which is 0011 1101
^
Binary XOR Operator copies the bit if it is set in one operand but not both.
(A ^ B) will give 49, which is 0011 0001
~
Binary Ones Complement Operator is unary and has the effect of 'flipping ' bits.
(~A ) will give -61, which is 1100 0011 in 2's complement due to a signed binary number
<< 
Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.
A << 2 will give 240, which is 1111 0000.
>> 
Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand
A >> 2 will give 15, which is 0000 1111


Example: 5
Input
Output
  • Other  Operators

Operator
Description
Example
sizeof()
Returns the size of a data type.
Sizeof(int), will return4
typeof()
Returns the type of a class.
Typeof(StreamReader)
&
Returns the address of a variable.
&a will give actual address of the variable
*
Pointer to a variable.
*a will pointer to a variable.
Ternary (? :)
Conditional Expression.
If Condition is true ? Then value X : Otherwise value Y
Is
Determines whether an object is of a certain type.
If( Dog is Animal) // checks if Cat is an object of the Animal class.
As
Cast without raising an exception if the cast fails
Object o = new String Reader("Hello"); String Reader r = o as String Reader;
+
Add (concatenate) two string values.
“Object”+”Oriented” = Object Oriented
.
Access the class members
Animal O = new Animal();         O.Cat();

Example: 6
Input
Output

  • Operator precedence: The operator precedence tells which operators are evaluated first. The precedence level is necessary to avoid ambiguity in expressions.

Example: 7 For example x = 5 + 2 * 4, here, x is assigned 13, not 40 because operator * has higher precedence than +, so it first gets multiplied with 2*4 and then adds into 7.

Expressions inside parentheses are always evaluated first so we can use parentheses to change the order of evaluation.

The following table shows common C# operators ordered by precedence (highest precedence first):

Operator(s)
Category
Associativity
Primary
x.y f(x) a[x] x++ x-- new typeof default checked unchecked
Right to Left
Unary
+ - ! ~ ++x --x (T)x
Right to Left
Multiplicative
* / %
Right to Left
Additive
+ -
Right to Left
Shift
<< >>
Right to Left
Equality
== !=
Left to Right
Logical AND
&
Right to Left
Logical XOR
^
Right to Left
Logical OR
|
Right to Left
Conditional AND
&&
Right to Left
Conditional OR
||
Right to Left
Null Coalescing
??
Right to Left
Ternary
?:
Left to Right
Assignment
= *= /= %= += -= <<= >>= &= ^= |= =>
Left to Right

----
@NetwaxLab

3 comments:

  1. Good Articles, Nice Job

    C#.NET interviews Questions and Answers

    http://interview-question-answer.weebly.com/cnet/c-interview-questions-and-answers

    ReplyDelete