Home » c# » && vs & and | vs ||… What’s the difference?

&& vs & and | vs ||… What’s the difference?

color-01 It seems like such a trivial thing to be talking about but not knowing the difference between && vs & or || vs | can make a huge difference between working code and code that only seems to work.

Let me illustrate:

        bool b = false;
        bool c = true;
        if(b & c)
            // do something

        if(b && c)
            // do something

In the code above, both b & c and b && c evaluate to false, so  we are safe.  No problems.  But this leads us to believe that the following code is also safe:

        string s = null;
        if(s != null & s.Length > 0)
            // do something

        if(s != null && s.Length > 0)
            // do something

and this is what would get you in trouble.

The single ampersand and single pipe are knows as “bitwise operators.”  What this means in practical terms is that they will take whatever they find on BOTH sides of the operator and AND (&) or OR(|) them together.

So in the case of:

if(s != null & s.Length > 0)

s.Length > 0 will still get evaluated even if s is null.  Not exactly what we had in mind.

There is another side effect of bitwise operators that gets used very infrequently.  Because they are bitwise, I can use them on integers as well as boolean values.

int i= 5 & 6;

is a perfectly legal construct in CSharp.  It will AND all the bits that make up 5  (101) and all the bits that make up 4 (100) and store the result in i (4 or 100).

On the other hand, && and || are strictly used for boolean expressions and will evaluate as little of the expression as they can get away with.

This is why we can write:

        if(s != null && s.Length > 0)
            // do something

If s is null, we already know that the expression will fail, so there is no need to evaluate the string’s length.

And now for one of my favorite binary statements.

There are 10 types of people in the world.  Those who understand binary and those who don’t.


Other post in c#

Related Post

  • JavaScript Boolean ConfusionJavaScript Boolean Confusion What could possibly be confusing about JavaScript Booleans you ask? Well, here’s several logical statements written in JavaScript.  Do you know what each does?     […]
  • CSharp EnumCSharp Enum It is often tempting when working with your code to test against static values in your system.  For example: if (i == 1) { // do something } else if (i == 2) { // do […]
  • SQL WHILE – SQL For ProgrammersSQL WHILE – SQL For Programmers The IF statement we looked at on Tuesday was pretty tame compared to the WHILE construct.Actually, the main thing you need to keep in mind is that WHILE is all you have.  There is no […]
  • What is the global keyword in CSharp?What is the global keyword in CSharp? During the Christmas break, I received the following question:What does C# global keyword actually do? Code example, from table adapter […]
  • Response.Redirect() executes too soon on the Server.Response.Redirect() executes too soon on the Server. I've seen this question a couple of times in various situations. The first involves Javascript and the second involves server side code. Both are caused by a misunderstanding of what this […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer focusing on ASP.NET, C#, Node.js, JavaScript, HTML, CSS, BootStrap, and Angular.JS.Does your team need additional help in any of the above? Contact Dave today.

2 Pingbacks/Trackbacks