Thursday, July 05, 2007

AndAnd and only And

From my little experience itself, I’ve seen various ‘demonic’ forms of condition that we put into ‘if’ (or ‘while’ loop). Sometime the combinations of ‘And’, ‘Or’ and ‘Not’s will scare hell out of you. But here is a classic case: Suppose you are applying ‘And’ condition and (in c# at least) you have used ‘&’ (which is bitwise operator) instead of ‘&&’. In most of the case this won’t give any problem unless you do something like this,

If( a != null & a.Count > 0)
{ do_something; }

It will catch you and exception will be thrown saying you want something which doesn’t exist.

NOTE: ‘&&’ is totally safe for above example as && will evaluate the second condition only when the first is true.

No comments: