I was picking at a work associate's code the other day. Turns out he was right and I was wrong. I thought that there was no guarantee on the order of evaluation of parts of a boolean expression. Take for example:
int a = 5;
if (a == 5 || (a / 0) == 1) { Debug.Print(”true statement”); }
I figured that the above might fail with a divide-by-zero exception in some cases, because maybe the compiler may evaluate the second half of the expression first.
I learned that it is guaranteed that evaluation will proceed from left to right, and will stop as soon as the truth or falsehood of the statement is known.
This bit of knowledge comes in handy when comparing a field in a DataSet with a value. For example:
if (!dsDataSet.TableName[0].IsFieldNull() && dsDataSet.TableName[0].Field == “foo”) { ... }
This code will work always, even if the field is null.
If you are a c# coder you may have already known this. It was not explicitly stated in the documentation, though, and I wanted to be sure.