c# - Order of operators in IF statement -


i when necessary prevent null pointer exception:

// example #1 if (cats != null && cats.count > 0) {   // } 

in #1, i've assumed cats != null needs first, because order of operations evaluate left right.

however, unlike example #1, want if object null or if count zero, therefore i'm using logical or instead of and:

// example #2 if (table == null || table.rows == null || table.rows.count <= 0) {   // } 

does order of logical comparisons matter? or can reverse order , same results, such in example #3?

// example #3 if (table.rows.count <= 0 || table.rows == null || table == null) {   // } 

(btw, realize can rewrite #2 below, think it's messy, , i'm still curious or operators)

// example #4 if (!(table != null && table.rows != null && table.rows.count > 0)) {   // } 

yes, short-circuiting happens in both cases, difference being && stops if lhs false (because overall expression must false) while || stops if lhs true (because overall expression must true).

the first 2 examples in question correct, third throw exception if table or table.rows null.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -