&& vs & and | vs ||… What’s the difference?
It seems like such a trivial thing to be talking about but not knowing the difference between the two operators 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 Advanced CSharp
- Two Interfaces. Same Method. Two meanings. - September 29th, 2008
- Readonly variables in CSharp? Really?! - October 29th, 2008
- Dispose with Using - November 10th, 2008
- Delegates in .NET - December 4th, 2008
- Using Sealed in CSharp - December 8th, 2008
- CSharp checked and unchecked - December 11th, 2008
- Advanced CSharp - unsafe mode - December 15th, 2008
- Volatile variables and CSharp threads - December 22nd, 2008
- What is the global keyword in CSharp? - December 29th, 2008
- CSharp fixed keyword - January 5th, 2009
- using - There's more there than you are using - February 2nd, 2009
- Removing Warnings from CSharp Compile Cycle - March 10th, 2009
- Just say “No!” to C# Regions? Really?! - April 16th, 2009
- C# “” better than string.Empty? - April 20th, 2009
- .Net String Pool – Not Just For The Compiler - April 22nd, 2009
- CSharp ?? Operator - May 18th, 2009
- Using VB.NET From CSharp - July 1st, 2009
- What is .NET’s Object.GetHashCode() Used For? - August 5th, 2009
- ASP.NET Substitution Control - October 22nd, 2009
- Transaction Tracking Typed Datasets Using SqlTransaction - July 20th, 2010
- CSharp's Property Shortcuts - July 17th, 2012
- && vs & and | vs ||... What's the difference? - August 21st, 2012
- Advanced CSharp - yield - November 27th, 2012
- Making values nullable - December 18th, 2012
- Stackalloc in CSharp - January 22nd, 2013
- Dispose, Finalize and SuppressFinalize - June 12th, 2013
Republished by Blog Post Promoter
Related Post
2 Pingbacks/Trackbacks
- 16 March 2009 at 10:03am
- Dew Drop - March 16, 2009 | Alvin Ashcraft's Morning Dew 17 March 2009 at 3:03am
- Reflective Perspective - Chris Alcock » The Morning Brew #308
[...] && vs & and | vs ||… What’s the difference? (Dave M. Bush) ...
[...] && vs & and | vs || - What’s the difference? - Dave explores the ...




Pingback: Dew Drop - March 16, 2009 | Alvin Ashcraft's Morning Dew
Pingback: Reflective Perspective - Chris Alcock » The Morning Brew #308