Click here to Skip to main content
15,887,485 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Logic Pin
Shameel17-May-11 7:52
professionalShameel17-May-11 7:52 
GeneralRe: Logic Pin
Shameel17-May-11 8:10
professionalShameel17-May-11 8:10 
GeneralRe: Logic [modified] PinPopular
gumi_r@msn.com18-May-11 23:35
gumi_r@msn.com18-May-11 23:35 
GeneralRe: Logic Pin
0bx18-May-11 21:05
0bx18-May-11 21:05 
GeneralRe: Logic PinPopular
AspDotNetDev17-May-11 11:02
protectorAspDotNetDev17-May-11 11:02 
GeneralRe: Logic Pin
Fabio V Silva17-May-11 11:30
Fabio V Silva17-May-11 11:30 
GeneralRe: Logic Pin
Quirkafleeg20-May-11 4:39
Quirkafleeg20-May-11 4:39 
GeneralRe: Logic PinPopular
_Erik_18-May-11 3:45
_Erik_18-May-11 3:45 
In the case you have posted the final result would be the same as if you had used the && operator. There is, however, an important difference between & and &&. Have a look at this example:

C#
ICollection col = null;
if (col != null && col.Count > 0)
    // Whatever


This would first check this expression: col != null; like it is false and we are using && operator, it would not even try to check the second operand, becouse the final result will be false. However, if we use just the bitwise & operator:

C#
ICollection col = null;
if (col != null & col.Count > 0)
    // Whatever


Since & operator is a bitwise operator it will try to check both operands so, in this case, it would throw a NullReferenceException when trying to evaluate the result of the second operand. That is why we always use the && operator in our boolean expressions, placing each operand in the right place. I guess this is what others have been trying to explain to you in this thread.

That said, I would not have downvoted your answer just for this if the rest of the answer is correct, and I think that not being able to make you understand this is not reason enough to tell them to RTFM.
GeneralRe: Logic Pin
Fabio V Silva18-May-11 4:09
Fabio V Silva18-May-11 4:09 
GeneralRe: Logic Pin
_Erik_18-May-11 4:29
_Erik_18-May-11 4:29 
GeneralRe: Logic Pin
Keith Barrow18-May-11 5:03
professionalKeith Barrow18-May-11 5:03 
GeneralRe: Logic Pin
Shameel18-May-11 23:55
professionalShameel18-May-11 23:55 
GeneralRe: Logic Pin
Nagy Vilmos19-May-11 0:04
professionalNagy Vilmos19-May-11 0:04 
GeneralRe: Logic Pin
gumi_r@msn.com20-May-11 0:02
gumi_r@msn.com20-May-11 0:02 
GeneralRe: Logic PinPopular
_Erik_20-May-11 4:09
_Erik_20-May-11 4:09 
GeneralRe: Logic Pin
OriginalGriff20-May-11 4:22
mveOriginalGriff20-May-11 4:22 
GeneralRe: Logic Pin
Nathan D Cook20-May-11 4:10
Nathan D Cook20-May-11 4:10 
GeneralRe: Logic Pin
OriginalGriff20-May-11 4:21
mveOriginalGriff20-May-11 4:21 
GeneralRe: Logic Pin
Manfred Rudolf Bihy20-May-11 4:38
professionalManfred Rudolf Bihy20-May-11 4:38 
GeneralRe: Logic Pin
OriginalGriff20-May-11 4:43
mveOriginalGriff20-May-11 4:43 
GeneralRe: Logic Pin
BobJanova20-May-11 4:46
BobJanova20-May-11 4:46 
GeneralRe: Logic Pin
Shameel25-May-11 8:31
professionalShameel25-May-11 8:31 
GeneralRe: Logic Pin
Nagy Vilmos20-May-11 5:13
professionalNagy Vilmos20-May-11 5:13 
GeneralRe: Logic Pin
sucram23-May-11 2:51
sucram23-May-11 2:51 
GeneralRe: Logic Pin
Timothy Byrd23-May-11 16:25
Timothy Byrd23-May-11 16:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.