jeudi 28 mai 2020

Operator precedence misunderstanding

Some of my knowledge about the topic is as following. If consecutive operators in an expression have the same precedence, a rule called associativity is used to decide the order in which those operators are evaluated. Further, left-associative operators of the same precedence are evaluated in order from left to right.

What I don't understand is why the following code doesn't throw exception.

if (

    object == null || 
    object.Flag && 
    object.Status == object2.Status

)

What if object is null? In this case, doesn't the call of object.Status throws exception because of high precedence of == over &&, likewise && over ||? I mean,

if (

    (object == null) || 
    (object.Flag && 
    (object.Status == object2.Status))

)

The call order,

  1. object.Status == object2.Status
  2. object.Flag
  3. object == null

What point do I overlook?

Aucun commentaire:

Enregistrer un commentaire