C# order of evaluation

Posted on 2009-02-17
Medium Priority
Last Modified: 2012-05-06
In C++, two things are true about an expression such as
   (a == b) || (c == d)

1)  The order of evaluation is not guaranteed, (c == d) could be evaluated before (a == b)
     (Section 5, paragraph 4 of the standard. "Except where noted, the order of evaluation (...words deleted...) is unspecified)
2)  If either (a == b) or (c == d) is true, then expression evaluation is shortcut and the other comparison is not evaluated.

Section 8.5 of the ECMA-366 C# standard says "Except for the assignment operators, all binary operators are left-associative"

1)  It looks to me as though (a == b) will always be evaluated first in C#.  Am I missing anything?
2)  If (a == b) is true, will C# evaluate (c == d)?
Question by:josgood
LVL 26

Accepted Solution

Anurag Thakur earned 2000 total points
ID: 23667161
1)  It looks to me as though (a == b) will always be evaluated first in C#.  Am I missing anything?
yes you are correct a==b will be evaluated first

2)  If (a == b) is true, will C# evaluate (c == d)?
if a==b will be true and an OR condition is there then c==d will NOT be evaluated

it saves processing time and will only be evaluated when a==b is False
LVL 13

Author Closing Comment

ID: 31548150
Thank you.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Screencast - Getting to Know the Pipeline

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question