Mysterious Boolean Behaviour

time to read 2 min | 211 words

I just got a test failing that should've worked. I checked into the source, and I found the following line of code:

bool hasValue = ReflectionUtil.HasValue(Value) && Value != defaultValue;

Debugging shows the following:

  •  ReflectionUtil.HasValue(Value) = true
  •  Value != defaultValue = false 
  •  ReflectionUtil.HasValue(Value) && Value != defaultValue = false

After stepping over this line, what is the value of hasValue?

  • hasValue = true

For the life of me I can't figure this out. This should be a basic boolean math, not something that would fail.

BTW, Value is a property with field.GetValue(parent), so it doesn't have any side effects that could cause it to break.