Short-circuit Boolean evaluation in C# ?

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2007
Posts: 57
Reputation: fishsqzr is an unknown quantity at this point 
Solved Threads: 1
fishsqzr fishsqzr is offline Offline
Junior Poster in Training

Short-circuit Boolean evaluation in C# ?

 
0
  #1
Nov 2nd, 2007
Does C# allow short-circuit evaluation of Boolean expressions? That is, if you have an expression such as
if (test1) & (test2) {....}
when test1 is false, there is no reason to evaluate test2 and a language which supports short-circuit evaluation never looks at the second test because the result will obviously be false. From what I can tell, C# doesn't do that, at least by default. Is there a compiler directive or attribute which will allow that?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Short-circuit Boolean evaluation in C# ?

 
1
  #2
Nov 2nd, 2007
Yes
Last edited by iamthwee; Nov 2nd, 2007 at 3:53 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 45
Reputation: _r0ckbaer is an unknown quantity at this point 
Solved Threads: 7
_r0ckbaer's Avatar
_r0ckbaer _r0ckbaer is offline Offline
Light Poster

Re: Short-circuit Boolean evaluation in C# ?

 
0
  #3
Nov 4th, 2007
Originally Posted by fishsqzr View Post
From what I can tell, C# doesn't do that, at least by default. Is there a compiler directive or attribute which will allow that?
So basically you think c# doesn't support short-circuit evaluation ?
Have a look here: http://msdn2.microsoft.com/en-us/lib...dk(VS.71).aspx

@iamthwee: Those kind of answers are not very helpful, altough they might amuse you (weird sense of humour). In case you intend to post such a crap again in the future, why don't you think twice about it and (possibly) keep out of the discussion?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 10
Reputation: jquick is an unknown quantity at this point 
Solved Threads: 3
jquick's Avatar
jquick jquick is offline Offline
Newbie Poster

Re: Short-circuit Boolean evaluation in C# ?

 
0
  #4
Nov 4th, 2007
Is that not what a SELECT / SWITCH / CASE statement is for? It essentially does the same thing.

Btw that was a stupid answer iamthwee

Jenni
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 57
Reputation: fishsqzr is an unknown quantity at this point 
Solved Threads: 1
fishsqzr fishsqzr is offline Offline
Junior Poster in Training

Re: Short-circuit Boolean evaluation in C# ?

 
0
  #5
Nov 5th, 2007
In that case, C# has a bug. I have a class with a string named FieldName. Until the string has a value assigned, it is null. I want to test for null, but I also want to see if the string has been assigned an empty string (a string with length zero) since there are situations where the user may erase its contents. So, I use the following

  1. if ((cr.FieldName == null) | (cr.FieldName.Length == 0))
  2. throw new Exception("Null FieldName in SQLNumStrategy");

If short-circuit is working right, it should never check the FieldName.Length property if FieldName is null becuase the result of the statement is already known. However, it does try to check, which raises a different exception (with the wrong message). I find I must break up the test into two separate if statements, testing for null first. Then, if FieldName is null, the correct exception is thrown and Length is never checked.

Other kinds short-circuit evaluations seem to work, so the bug appears related to the test for null. I guess this is just yet another C# 'feature'.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 45
Reputation: _r0ckbaer is an unknown quantity at this point 
Solved Threads: 7
_r0ckbaer's Avatar
_r0ckbaer _r0ckbaer is offline Offline
Light Poster

Re: Short-circuit Boolean evaluation in C# ?

 
0
  #6
Nov 5th, 2007
The short-circuit is working right, however your code is not, a short-circuit evaluation should be written like this :

  1. if ((cr.FieldName == null) || (cr.FieldName.Length == 0))
  2. throw new Exception("Null FieldName in SQLNumStrategy");
Note the 2 pipes (which represent a conditional-OR)
The framework proposes also a convenience method for your particular case which is string.IsNullOrEmpty():
http://msdn2.microsoft.com/en-us/lib...llorempty.aspx
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 45
Reputation: _r0ckbaer is an unknown quantity at this point 
Solved Threads: 7
_r0ckbaer's Avatar
_r0ckbaer _r0ckbaer is offline Offline
Light Poster

Re: Short-circuit Boolean evaluation in C# ?

 
0
  #7
Nov 6th, 2007
That is of course only in case FieldName is of type string
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 57
Reputation: fishsqzr is an unknown quantity at this point 
Solved Threads: 1
fishsqzr fishsqzr is offline Offline
Junior Poster in Training

Re: Short-circuit Boolean evaluation in C# ?

 
0
  #8
Nov 6th, 2007
Thanks. That was a stupid mistake on my part, and I didn't know about IsNullOrEmpty at all.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Short-circuit Boolean evaluation in C# ?

 
0
  #9
Nov 7th, 2007
I merely answered the question posed.
Last edited by iamthwee; Nov 7th, 2007 at 1:59 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC