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 tofalse
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 isbool
. The lifted form is constructed by adding a single?
modifier to each operand type. The lifted operator produces the valuefalse
if one or both operands are null. Otherwise, the lifted operator unwraps the operands and applies the underlying operator to produce thebool
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 int
s, 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.
a.Right == b.Right // bool
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
aa == bb // bool
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
Calculated Column in Ef Code First
How to Get Urls of Open Pages from Chrome and Firefox
Use of SQLparameter in SQL Like Clause Not Working
Why Is It Considered Bad to Expose List<T>
Checking If a String Array Contains a Value, and If So, Getting Its Position
.Contains() on a List of Custom Class Objects
Getproperties() to Return All Properties for an Interface Inheritance Hierarchy
Test If a Website Is Alive from a C# Application
Get Thumbnail Image of Video File in C#
Is There a Way of Setting a Property Once Only in C#
Get the Property, as a String, from an Expression<Func<Tmodel,Tproperty>>