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 6560487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:28:00+00:00 2026-05-25T13:28:00+00:00

Why is && preferable to & and || preferable to | ? I asked

  • 0

Why is && preferable to & and || preferable to |?

I asked someone who’s been programming for years and his explanation was:

For example, in if (bool1 && bool2 && bool3) { /*DoSomething*/ }, bool1 has to be true for it to test bool2 which has to be true before moving on to bool3, etc. If I’d used a single & instead there is no order to the test even if all of them have to be true to progress to the next line, so why does it matter anyway?

Note: I’d like to point out that I’m the programming equivalent of a toddler and this is not a serious or urgent question. It’s more a matter of understanding why things should be done a certain way as opposed to another.

  • 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-05-25T13:28:00+00:00Added an answer on May 25, 2026 at 1:28 pm

    In most cases, && and || are preferred over & and | because the former are short-circuited, meaning that the evaluation is canceled as soon as the result is clear.

    Example:

    if(CanExecute() && CanSave())
    {
    }
    

    If CanExecute returns false, the complete expression will be false, regardless of the return value of CanSave. Because of this, CanSave is not executed.

    This is very handy in the following circumstance:

    string value;
    if(dict.TryGetValue(key, out value) && value.Contains("test"))
    {
        // Do Something
    }
    

    TryGetValue returns false if the supplied key is not found in the dictionary. Because of the short-circuiting nature of &&, value.Contains("test") is only executed, when TryGetValue returns true and thus value is not null. If you would use the bitwise AND operator & instead, you would get a NullReferenceException if the key is not found in the dictionary, because the second part of the expression is executed in any case.

    A similar but simpler example of this is the following code (as mentioned by TJHeuvel):

    if(op != null && op.CanExecute())
    {
        // Do Something
    }
    

    CanExecute is only executed if op is not null. If op is null, the first part of the expression (op != null) evaluates to false and the evaluation of the rest (op.CanExecute()) is skipped.

    Apart from this, technically, they are different, too:
    && and || can only be used on bool whereas & and | can be used on any integral type (bool, int, long, sbyte, …), because they are bitwise operators. & is the bitwise AND operator and | is the bitwise OR operator.

    To be very exact, in C#, those operators (&, | [and ^]) are called “Logical operators” (see the C# spec, chapter 7.11). There are several implementations of these operators:

    1. For integers (int, uint, long and ulong, chapter 7.11.1):
      They are implemented to compute the bitwise result of the operands and the operator, i.e. & is implement to compute the bitwise logical AND etc.
    2. For enumerations (chapter 7.11.2):
      They are implemented to perform the logical operation of the underlying type of the enumeration.
    3. For bools and nullable bools (chapter 7.11.3 and 7.11.4):
      The result is not computed using bitwise calculations. The result is basically looked up based on the values of the two operands, because the number of possibilities is so small.
      Because both values are used for the lookup, this implementation isn’t short-circuiting.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

K&R C Programming Language: pg. 105 Extend entab and detab to accept the shorthand
I can see this question (or a similar one) has been asked a few
& has && . | has || . Why doesn't ^ have ^^ ?
& Eacute ; \u00C9 & egrave ; \u00E8 & eacute ; \u00E9 & apos
  shows some symbol istead of space in IE? could you tell me why?
Thanks to a Q&A on stackoverflow. I just found out how to determine the
When I drag & drop a dll to the assembly folder on vista, I
Is there a quick & dirty way of obtaining a list of all the
Possible Duplicate: Pre & post increment operator behavior in C, C++, Java, & C#
User-Defined Functions & Collating Sequences Full support for user-defined functions and collating sequences means

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.