| | |
Short-circuit Boolean evaluation in C# ?
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Aug 2007
Posts: 57
Reputation:
Solved Threads: 1
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?
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?
•
•
•
•
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?
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?
•
•
Join Date: Aug 2007
Posts: 57
Reputation:
Solved Threads: 1
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
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'.
C# Syntax (Toggle Plain Text)
if ((cr.FieldName == null) | (cr.FieldName.Length == 0)) 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'.
The short-circuit is working right, however your code is not, a short-circuit evaluation should be written like this :
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
C# Syntax (Toggle Plain Text)
if ((cr.FieldName == null) || (cr.FieldName.Length == 0)) throw new Exception("Null FieldName in SQLNumStrategy");
The framework proposes also a convenience method for your particular case which is string.IsNullOrEmpty():
http://msdn2.microsoft.com/en-us/lib...llorempty.aspx
![]() |
Similar Threads
- PHP4: stupid loop + and 'optimization'?? (PHP)
- Need Help in Recursion (C)
- Logical operations and data-checks (Java)
- Horizontal lines across display (Monitors, Displays and Video Cards)
- Dr Scheme Tutorial (Computer Science)
- i can't figure out this code :( help please (C)
- Vertical colours lines in my screen laptop (please help!) (Monitors, Displays and Video Cards)
Other Threads in the C# Forum
- Previous Thread: form level variable
- Next Thread: More stupid questions
| Thread Tools | Search this Thread |
.net access algorithm array asp.net barchart bitmap box broadcast c# check checkbox client combobox control conversion csharp custom database databaseconnection datagrid datagridview dataset datetime dbconnection degrees design development draganddrop drawing encryption enum event eventhandlers excel file firefox form format forms function gdi+ grantorrevokepermissionthroughc#.net httpwebrequest image index input install java label libraries list listbox loop mandelbrot marshalbyrefobject math mouseclick movingimage mysql mysql.data.client operator path photoshop picturebox pixelinversion post programming radians regex remote remoting resourcefile richtextbox server sleep socket sql statistics stream string study system.servicemodel table tcpclientchannel text textbox thread time timer update usercontrol validation visualstudio webbrowser windows winforms wpf wpfc# xml







