Friday, May 22, 2015

C# - Logical, And, Or, IS, AS Easy Explanation

Bit wise AND: &

& - tests both sides every time.

Bit wise OR: |

| - test both sides every time.

The & and | above are bit wise operators they can operate on both integer and Boolean arguments, and && and || are logical operators that can operate only on Boolean arguments. In many languages, if both arguments are Boolean, the key difference is that the logical operators (see below) will perform short circuit evaluation and not evaluate the second argument if the first argument is enough to determine the answer (e.g. in the case of &&, if the first argument is false, the second argument is irrelevant).
XYX && Y
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse
XYX || Y
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse
X!X
truefalse
falsetrue
Short-Circuiting
When evaluating an expression involving && or || operators, C# uses a practice called short-circuiting. This means that C# will try to do as little work as possible when evaluating a boolean expression. The code "short-circuits" differently depending on the operator being used.
For example, consider this expression:
x || y || z
If x is true, then C# won't even consider the values of y or z, because when using || only one of the values must be true in order for the entire expression to evaluate to true. Look at the truth table for || to see this fact.
Likewise, when using &&:
x && y && z
If x is false, then C# won't look at the values of y or z, because when using && only one of the values must be false in order for the entire expression to evaluate to false. Look at the truth table for && to see this fact.
This is especially important to understand when the conditional expression contains more complex code. Consider this example:
x || SomeMethodThatReturnsABoolean()
If x is true, then the method will not be run. If x is false, then C# will continue evaluating the expression looking for the first operand that evaluates to true, and the method will be run as part of that evaluation.
In general it's best practice to keep conditional expressions as simple as possible. There are a few examples of times when short-circuiting can be very helpful. Consider the following example where I only want to do something if the names array contains at least one item.
if(names != null && names.Length > 0)
{
    // Do something with names
}
Without short-circuiting, accessing the Length property of the names array would throw an exception if names wasnull. But with short-circuiting, names.Length will never get run if names is null so it's safe to put both these expressions together. Just remember that order is important. Boolean expressions are evaluated from left to right, with respect to parenthesis of course.

 The Is Cast operator:

A casting operator.
Is will return true if the left side object can be CAST to the right. Otherwise false...
 if(q is rectangle){ then...}  


 The AS Cast operator:

The "As" operator: retuns null if it fails casting: 
 rectangleObject rekt = someobject as rectangleObject 
// invalid cast, returns null otherwise it will cast.  

No comments:

Post a Comment