Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does C# evaluates AND OR expression with no brackets

Tags:

c#

.net

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 .

like image 398
Eatdoku Avatar asked Dec 02 '22 04:12

Eatdoku


1 Answers

&& has a higher precedence than || so it's evaluated first. Effectively, they're equivalent to:

false && true || false  =>  (false && true) || false  =>  false
false || true && false  =>  false || (true && false)  =>  false

If you're unsure, use the parentheses. They have no real negative impact and anything that makes code more readable is generally a good thing.

Perhaps a better example (so that the results are different) would have been:

true && false || false  =>  (true && false) || false  =>  false
true || false && false  =>  true || (false && false)  =>  true
like image 196
paxdiablo Avatar answered Dec 05 '22 09:12

paxdiablo