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
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)
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;
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#
- Assign Multiple enum Values To One Variable - November 2nd, 2007
- Simple Properties in C# 3.5 - November 22nd, 2007
- CSharp adds the var keyword! - November 27th, 2007
- Object Initialization in CSharp 3.0 and VB.NET 9 - November 28th, 2007
- Computed Columns Using LINQ to SQL Classes - February 12th, 2008
- DataSets, ObjectDataSource and 3-Tier ASP.NET - February 29th, 2008
- Serializable attribute vs Serializable Interface - April 9th, 2008
- Why you can't cast an integer to a string. - June 23rd, 2008
- Unsafe Mode in C# - December 15th, 2008
- What is the global keyword in CSharp? - December 29th, 2008
- 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
- Upload a File via WebRequest Using CSharp - November 10th, 2009
- Azure CDN Blob Storage Case Sensitive Issue - April 17th, 2012
- && vs & and | vs ||... What's the difference? - August 21st, 2012
- Decimal vs Float (Single) or Double - September 18th, 2012
- C# Self Executing Anonymous Function - February 28th, 2013
- Dispose, Finalize and SuppressFinalize - June 12th, 2013
- CSharp Enum - October 2nd, 2013
- CSharp Numeric Overflows - July 3rd, 2014
- CSharp IDisposable Confusion - July 17th, 2014
- Why Get Certified? - July 24th, 2014
- C# Properties Get and Set - November 13th, 2014
- Value Type vs Reference Type - November 27th, 2014
- String and StringBuilder - December 11th, 2014
- Browser Automation in .NET w/ Chromium - July 9th, 2015
- CefSharp Offscreen [Why do I have so many instances of Chromium?] - October 1st, 2015