Operators in C#
Every programming language must have Arithmetic Operators. Given below are the Arithmetic Operators used in C#Operand | Description |
---|---|
+ | Addition |
- | Subtract |
* | Multiply |
/ | Divide |
% | Modulo |
++ | Increment By 1 |
-- | Decrement By 1 |
Assignment Operator
To transfer values from one variable to another or to assign vales to a variable we use these operators,Operand | Description |
---|---|
= | Simple assignment |
-= | Subtract assignment |
*= | Multiply assignment |
/= | Divide assignment |
%= | Modulo assignment |
+= | Additive assignment |
<<= | left-shift assignment operator |
>>= | left-shift assignment operator |
&= | AND assignment operator |
^= | exclusive-OR assignment operator |
|= | OR assignment operator |
?? | The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand |
Relational Operator
In a condition when we need to compare two variables or values. These operators are used in such casesOperand | Description |
---|---|
== | Equality |
> | Greater than |
< | Less than |
!= | Not equals |
>= | Greater than equal to |
<= | Less than equal to |
Conditional Operator
These are operators used to combine two or more conditions to produce one single resultThere are basically two logical operators &&(and) ||(or) . We will read there usage a bit later
But to clarify my point, let us assume you have one condition A and another condition B, each of these condition can only be true or false, thus the number of permutation becomes 4. So if we use logical operator we can combine them to produce one single output
Usage Of OR
Condition A | Condition B | A OR B |
---|---|---|
false | false | false |
true | false | true |
false | true | true |
true | true | true |
Usage Of AND
Condition A | Condition B | A AND B |
---|---|---|
false | false | false |
true | false | false |
false | true | false |
true | true | true |
Lets look at an example to illustrate my point
/** * Author : vblord * Date : 07/03/2011 * Difficulty : Very Easy * Description : If a student is older than 18 years and a citizen of India * only then he/she is permitted to open an account in our bank. * This program shows the usage of logical operator AND */ using System; namespace HelloWorld { class LogicalOperand { static void Main(string[] args) { Console.Write("Enter age of the student : "); int age = System.Int32.Parse(Console.ReadLine()); Console.Write("Are you a citizen of India (Y/N) : "); char citizen = char.Parse(Console.ReadLine()); if (age > 18 && citizen == 'Y'){ Console.WriteLine("Hello student you are elligible to open a bank account with us"); } else { Console.WriteLine("Hello student you are NOT ALLOWED to open a bank account with us"); } Console.ReadKey(false); } } }
Bitwise/Logical Operator
These are operators work on each bit of the operands if they are not boolean. It is very useful when each bit packs some information and we nned to read each of themOperand | Description |
---|---|
& | Logical AND |
| | Logical OR |
! | Logical NOT |
^ | Logical XOR |
Lets investigate & a bit more deeply. Here are the few salient points
The unary & operator returns the address of its operand (requires unsafe context).
Binary & operators are predefined for the integral types and bool. For integral types, & computes the logical bitwise AND of its operands. For bool operands, & computes the logical AND of its operands; that is, the result is true if and only if both its operands are true.
The & operator evaluates both operators regardless of the first one's value.
/** * Author : vblord * Date : 07/03/2011 * Difficulty : Very Easy * Description : Shows the usage of Bitwise & with Integer and Boolean values */ using System; class MainClass { static void Main() { Console.WriteLine(true & false); // logical and Console.WriteLine(true & true); // logical and Console.WriteLine("0x{0:x}", 0xf8 & 0x3f); // bitwise and } }
Shift Operator
There are two shift operators << (left shift) and >> (right shift) The shift the first operand left or right by the number of bits specified in the second operand.>>
If the first operand is an int or uint (32-bit quantity), the shift count is given by the low-order five bits of the second operand (second operand & 0x1f).
If the first operand is a long or ulong (64-bit quantity), the shift count is given by the low-order six bits of the second operand (second operand & 0x3f).
If the first operand is an int or long, the right-shift is an arithmetic shift (high-order empty bits are set to the sign bit). If the first operand is of type uint or ulong, the right-shift is a logical shift (high-order bits are zero-filled).
<<
If first operand is an int or uint (32-bit quantity), the shift count is given by the low-order five bits of second operand.
If first operand is a long or ulong (64-bit quantity), the shift count is given by the low-order six bits of second operand.
The high-order bits of first operand are discarded and the low-order empty bits are zero-filled. Shift operations never cause overflows.
Additionally there are few more operators used by C# namely [] which is used for arrays, indexers, and attributes. They can also be used with pointers.
()In addition to being used to specify the order of operations in an expression, parentheses are used to specify casts, or type conversions
The dot operator (.) is used for member access. The dot operator specifies a member of a type or namespace. For example, the dot operator is used to access specific methods within the .NET Framework class libraries:
The ternary operator (?:) returns one of two values depending on the value of a Boolean expression. The conditional operator is of the form
condition ? first_expression : second_expression;
If condition is true, first expression is evaluated and becomes the result; if false, the second expression is evaluated and becomes the result. Only one of two expressions is ever evaluated.
Calculations that might otherwise require an if-else construction can be expressed more concisely and elegantly with the conditional operator. For example, to avoid a division by zero in the calculation of the sin function you could write either
// The class Console in namespace System:
System.Console.WriteLine("hello");
No comments:
Post a Comment