I often do this when necessary to throw a null pointer exception:
if (cats != null && cats.Count > 0)
{
}
In # 1, I always assumed that I cats != nullshould be the first, because the order of operations is evaluated from left to right.
However, unlike example # 1, now I want to do something if the object nullor if it Countis equal to zero, so I use logical OR instead of AND:
if (table == null || table.Rows == null || table.Rows.Count <= 0)
{
}
Does the order of logical comparisons mean? Or can I also change the order and get the same results as in example # 3?
if (table.Rows.Count <= 0 || table.Rows == null || table == null)
{
}
(By the way, I understand that I can rewrite # 2 as shown below, but I think this is messy and I'm still curious about OR statements)
if (!(table != null && table.Rows != null && table.Rows.Count > 0))
{
}