How Does Comparison Operator Works with Null Int

How does comparison operator works with null int?

According to MSDN - it's down the page in the "Operators" section:

When you perform comparisons with nullable types, if the value of one of the nullable types is null and the other is not, all comparisons evaluate to false except for !=

So both a > b and a < b evaluate to false since a is null...

Comparing 'int' to 'null' compiles

When the comparison is made, the compiler tries to make it so both operands of the comparison have compatible types if possible.

It had an int value and a constant null value (with no particular type). The only compatible type between the two values is int? so they are coerced to int? and compared as int? == int?. Some int value as an int? is definitely non-null and null is definitely null. The compiler realizes that and since a non-null value is not equal to a definite null value, the warning is given.

Why does default comparer consider null to be less than a value

It needs to have a defined comparison in terms of IComparable<T> / IComparer<T>, and it has elected that null is less than anything else. This is specified here. The only other option would have been to throw an exception, which most people would consider a bad thing here, as it would break any sorting with nulls in.

The < operator is entirely separate, and has elected to return false when either operand is null. As non-intuitive as it seems: CompareTo and < do not need to agree precisely.

How do nullable types handle null values with comparison operators?

Does anyone have concrete information on how C# handles comparisons with Nullable types when one side of the comparison is null?

Yes - the C# language specification, section 7.3.7. In this case, it's a relational operator:

For the relation operators < > <= >= a lifted form of an operator exists if the operand types are both non-nullable types and if the result type is bool. The lifted form is constructed by adding a single ? modifier to each operand type. The lifted operator produces the value false if one or both operands are null. Otherwise, the lifted operator unwraps the operands and applies the underlying operator to produce the bool result.

There are similarly detailed sections for other operators.

When in doubt about how some aspect of the language works (and whether it's guaranteed or implementation-specific), the C# language specification should be your first port of call.

Is Nullableint a Predefined value type - Or how does Equals() and == work here?

In C#, there's a concept called "Lifted Operators", described in section 7.3.7 of the language specification (Version 5 download):

Lifted operators permit predefined and user-defined operators that operate on non-nullable value types to also be used with nullable forms of those types. Lifted operators are constructed from predefined and user-defined operators that meet certain requirements, as described in the following

And specifically:

For the equality operators

==  !=

a lifted form of an operator exists if the operand types are both non-nullable value types and if the result type is bool. The lifted form is constructed by adding a single ? modifier to each operand type. The lifted operator considers two null values equal, and a null value unequal to any non-null value. If both operands are non-null, the lifted operator unwraps the operands and applies the underlying operator to produce the bool result.

So, since there's an == operator defined between ints, there's also one defined for int?s

C# comparing reference and value types with null

In short, you can't compare like this, ever...

In regards to if (a.Right == b.Right == null)

It compiles because you can compare a non-nullable value types with null and it's always false.

  1. a.Right == b.Right // bool

  2. bool == null //will be lifted and always equal false (see below)

To know the reason why you'll have to visit the specs and understand lifted operators

12.4.8 Lifted operators

Lifted operators permit predefined and user-defined operators that
operate on non-nullable value types to also be used with nullable
forms of those types

  • For the equality operators == !=

a lifted form of an operator exists if the operand types are both
non-nullable value types and if the result type is bool
. The lifted
form is constructed by adding a single ? modifier to each operand
type. The lifted operator considers two null values equal, and a null
value unequal to any non-null value
. If both operands are non-null,
the lifted operator unwraps the operands and applies the underlying
operator to produce the bool result.

In regards to if(aa == bb == 0) you can't compare bool with an int, there is no implicit conversion to bool

  1. aa == bb // bool

  2. bool == 0 // there is no implicit conversion to bool

What is the cleanest way to compare an int with a potentially null Integer in Java?

I try to avoid casts whenever possible, so I'd rather use the following, which also looks nicer in my opinion:

Integer.valueOf(8).equals(m.get("null"))

MySQL comparison with null value

In MySQL, NULL is considered as a 'missing, unknown value', as opposed to no value. Take a look at this MySQL Reference on NULL.

Any arithmetic comparison with NULL does not return true or false, but returns NULL instead., So, NULL != 'C' returns NULL, as opposed to returning true.

Any arithmetic comparison with 'NULL' will return false. To check this in SQL:

SELECT IF(NULL=123,'true','false') 

To check NULL values we need to use IS NULL & IS NOT NULL operator.

Why does it make sense in C# to compare nullable and non-nullable int?

A lot has been written about "lifting" operations in C# (eg. here), where operators with Nullable<T> arguments are treated as operators on T when all operands are non-null. And null is only equivalent to itself.

usually fetched from DB as allowing nulls but known to me never to be at that value

In which case why is the column not set to not null?

The lifting is there because so many databases have nullable columns when they should not be.



Related Topics



Leave a reply



Submit