C# order of evaluation

Posted on 2009-02-17
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

    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

    Thank you.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now