Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8731397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:13:46+00:00 2026-06-13T09:13:46+00:00

I know that || and && are defined as short-circuit operators in C#, and

  • 0

I know that || and && are defined as short-circuit operators in C#, and such behaviour is guaranteed by the language specification, but do |= and &= short-circuit too?

For example:

private bool IsEven(int n)
{
    return n % 2 == 0;
}

private void Main()
{
    var numbers = new int[] { 2, 4, 6, 8, 9, 10, 14, 16, 17, 18, 20 };

    bool allEven = true;
    bool anyOdd = false;

    for (int i = 0; i < numbers.Length; i++)
    {
        allEven &= IsEven(numbers[i]);
        anyOdd |= !IsEven(numbers[i]);
    }
}

When the 9 entry is hit, allEven becomes false, meaning that all subsequent entries are irrelevant – the value of allEven is guaranteed to be false for all future calls to that expression. The same goes for anyOdd, which is set to true when it sees 9, and will remain true for all subsequent calls to that expression.

So, do &= and |= shortcut, or is IsEven guaranteed to be called on every iteration? Is there any defined behaviour in the language specification for this case? Are there any corner-cases where such short circuiting would be problematic?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:13:48+00:00Added an answer on June 13, 2026 at 9:13 am

    The C# specification guarantees that both sides are evaluated exactly once from left-to-right and that no short-circuiting occurs.

    5.3.3.21 General rules for expressions with embedded expressions

    The following rules apply to these kinds of expressions: parenthesized expressions (§7.6.3), element access expressions (§7.6.6), base access expressions with indexing (§7.6.8), increment and decrement expressions (§7.6.9, §7.7.5), cast expressions (§7.7.6), unary +, -, ~, * expressions, binary +, -, *, /, %, <<, >>, <, <=, >, >=, ==, !=, is, as, &, |, ^ expressions (§7.8, §7.9, §7.10, §7.11), compound assignment expressions (§7.17.2), checked and unchecked expressions (§7.6.12), plus array and delegate creation expressions (§7.6.10).

    Each of these expressions has one or more sub-expressions that are unconditionally evaluated in a fixed order.

    The C# specification for compound operators says:

    7.17.2 Compound assignment

    …

    An operation of the form x op= y is processed by applying binary operator overload resolution (§7.3.4) as if the operation was written x op y. Then,

    • If the return type of the selected operator is implicitly convertible to the type of x, the operation is evaluated as x = x op y, except that x is evaluated only once.

    • Otherwise, if the selected operator is a predefined operator, if the return type of the selected operator is explicitly convertible to the type of x, and if y is implicitly convertible to the type of x or the operator is a shift operator, then the operation is evaluated as x = (T)(x op y), where T is the type of x, except that x is evaluated only once.

    …

    In your case op is & or |. The short circuiting behavior mirrors that of &/| and not &&/||.


    Note that this only refers to behavior visible in a single threaded scenario. So if the right hand side has no side-effects that are observable in such a scenario, the compiler or JITter is still free to omit the evaluation.

    In your example the compiler is free to terminate the loop once it knows the result, since there are no such side-effects. But it’s not required to do so.

    In particular timing does not count as such a side-effect, and you thus can’t rely on your code having constant runtime. This can be problematic in a security context, since it can introduce a timing side-channel.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that && and || are used in comparisons as AND and OR
I know that many threads has been created here & on the internet about
I know that I can get word completion through CTRL+N & CTRL+P and code
I know that Phonegap has an event for back button, but it's only available
I know that this sort of question has been asked here before, but still
I know that Java have its own garbage collection, but sometimes I want to
I know that the postfix versions of the increment/decrement operators will generally be optimised
In JavaScript, I know that a closure is can be defined as a nested
I know that if port 443 is open that means the remote host supports
I know that when I use range([start], stop[, step]) or slice([start], stop[, step]) ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.