Why does >= return false when == returns true for null values?

前端 未结 8 2050
遥遥无期
遥遥无期 2020-12-07 18:17

I have two variables of type int? (or Nullable if you will). I wanted to do a greater-than-or-equal (>=) comparison on the two variables but as it turns out, this

相关标签:
8条回答
  • 2020-12-07 19:00

    NULL is not zero (numeric or binary value), a zero-length string, or blank (character value). So any comparison operator will always return false on it. Read more about it here

    0 讨论(0)
  • 2020-12-07 19:02

    Since by default an int cannot be null and its value will be set to 0, the operator of > and < which is built for int type, expects to work with values and not with nulls.

    see my answer to a similar question where I wrote some ways to handle nullable int with the less < and greater > operators https://stackoverflow.com/a/51507612/7003760

    0 讨论(0)
提交回复
热议问题