Difference in && and &=
Messages   Related Types
This message was discovered on microsoft.public.dotnet.languages.csharp.

Post a new message to this list...

Joel
Run this method:

public void test()
{
bool b;
int i=0;

b=false;
i=0;
b=(b && i++==1);
Console.WriteLine(i.ToString());

b=false;
i=0;
b&=i++==1;
Console.WriteLine(i.ToString());
}

Notice how in the first syntax, i++ is NOT evaluated (because of
short-circuit evaluation) but in the second syntax i++ is evaluated. I
wonder if this was intentional or an oversight. It burned me in a situation
when I was counting on short-circuit evaluation.

<joel>

Reply to this message...
 
    
Chris R. Timmons
"Joel" <Click here to reveal e-mail address> wrote in
news:eIfF$Click here to reveal e-mail address:

[Original message clipped]

Joel,

& and && are two completely different operators. & performs a
bitwise "and" of two numbers, but && is the boolean "and" operator.
Change the && to & and both code blocks return the same result (1).

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Reply to this message...
 
    
Xarky
"Chris R. Timmons" <Click here to reveal e-mail address> wrote in message news:<Xns95639CAC22BA5crtimmonscrtimmonsin@207.46.248.16>...
[Original message clipped]

Is short-circuit evaluation the same as lazy evaluation?
Reply to this message...
 
    
Chris R. Timmons
Click here to reveal e-mail address (Xarky) wrote in
news:Click here to reveal e-mail address:

[Original message clipped]

http://en.wikipedia.org/wiki/Lazy_evaluation

and

http://en.wikipedia.org/wiki/Short_circuit

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Reply to this message...
 
    
Joel
Yeah, I'm aware of the difference between & and && (and and and? ;-). Just
pointing out a 'gotcha' that 'gotme'.

</joel>

"Chris R. Timmons" <Click here to reveal e-mail address> wrote in message
news:Xns95648D90EBDEFcrtimmonscrtimmonsin@207.46.248.16...
[Original message clipped]

Reply to this message...
 
 
System.Console




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com