OPERATORS IN C# 2.0
Different types of operators are:
1. Arithmetic 2. Bitwise 3. Relational and logical 4. Assignment Operator 5. ? Operator
1. Arithmetic
Operator Meaning + Addition - Subtraction * Multiplication / Division % Modulus ++ Increment -- Decrement
The operators +, -, *, and / all work the same way in C# as they do in any other computer language (or in algebra, for that matter). These can be applied to any built-in numeric data type.
2. Bitwise
Operator Result & Bitwise AND | Bitwise OR ^ Bitwise XOR >> Shift Right << Shift Left ~ One’s complement (unary NOT) They perform Boolean logical operations.
3. Relational and logical
Relational operator produces true/false result .They often work with logical operator.
Operator Meaning = = Equal to != Not equal to > Greater than < Less than >= Greater than or equal to <= Less than or equal to
The logical operators, the operands must be of type bool, and the result of a logical operation is of type bool.
Operator Meaning & AND | OR ^ XOR (exclusive OR) || Short-circuit OR && Short-circuit AND ! NOT
Here most of them came with a doubt that. What is the difference between & and && or | and ||.
int d=1,n=10;
if (d != 0 && (n % d) == 0) Console.WriteLine(d + " is a factor of " + n);
/* Now, try the same thing without short-circuit operator. This will cause a divide-by-zero error. */ d = 0; n = 10; if (d != 0 & (n % d) == 0) Console.WriteLine(d + " is a factor of " + n);
Implies
Implies is the new logical operator in C#. It works as follows:
p q p implies q True True True True False False False False True False True True
4. Assignment Operator
The assignment operator is the single equal sign, =.
Example : x=y=10;
5. ? Operator
This is also known as ternary operator.
Syntax:
Exp1? Exp2:Exp3
If the Exp1 is true it returns the value of Exp2.Else it returns the value of Exp3.
Sample code:
int i=1,j;
j = i > 0 ? 2 : 3; // j = 2 because i>0 is true
|
No responses found. Be the first to respond and make money from revenue sharing program.
|