not sure if this make sense at all im trying to understand how C# process the following logic
false && true || false
false || true && false
basically i'm trying to find out how C# evaluate these expression when there is no parentheses .
not sure if this make sense at all im trying to understand how C# process the following logic
false && true || false
false || true && false
basically i'm trying to find out how C# evaluate these expression when there is no parentheses .
The the AND operator has higher precedence than the OR operator (see http://msdn.microsoft.com/en-us/library/6a71f45d.aspx). Meaning && is always evaluated first.
Thus
x && y || z
is understood to be(x && y) || z
.And
a || b && c
is understood to bea || (b && c)
.C# Operators shows operator precedence:
&&
(logical AND) has higher precedence than||
(logical OR)NOTE: it is good practice (some might say best practice) to always use parentheses to group logical expressions, so that the intention is unambiguous...
Everyone said about the operator precedence and the reference tables where you can look that up. But I'd like to give a hint how to remember it. If you think of
false
as of0
and oftrue
as of1
then&&
is like multiplication and||
is like addition (they are actually called logical multiplication and logical addition). The precedence relationship is the same: multiplication is higher then addition. It works the same way:(*) it's actually 2 capped at 1
And normally, when in doubt, use parenthesis.
The operators will be evaluated in order of operator precedence.
So, basically,
AND
beforeOR
s. Your examples are the same as:Most importantly... C# uses short circuit operators. So that entire thing is false link text
operator precedence short circuit evaluation parenthesis left to right.