C# Nullable Equality Operations, Why does null <

2019-04-28 20:30发布

This question already has an answer here:

Why is it that in .NET

null >= null

resolves as false, but

null == null 

resolves as true?

In other words, why isn't null >= null equivalent to null > null || null == null?

Does anyone have the official answer?

11条回答
Ridiculous、
2楼-- · 2019-04-28 20:53

When I run

null >= null

I get a warning saying:

Comparing with null of type 'int?' always produces 'false'

I wonder why it is casted to an int though.

查看更多
一纸荒年 Trace。
3楼-- · 2019-04-28 20:55

This behaviour is defined in the C# specification (ECMA-334) in section 14.2.7 (I have highlighted the relevant part):

For the relational 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 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.

In particular, this means that the usual laws of relations don't hold; x >= y does not imply !(x < y).

Gory details

Some people have asked why the compiler decides that this is a lifted operator for int? in the first place. Let's have a look. :)

We start with 14.2.4, 'Binary operator overload resolution'. This details the steps to follow.

  1. First, the user-defined operators are examined for suitability. This is done by examining the operators defined by the types on each side of >=... which raises the question of what the type of null is! The null literal actually doesn't have any type until given one, it's simply the "null literal". By following the directions under 14.2.5 we discover there are no operators suitable here, since the null literal doesn't define any operators.

  2. This step instructs us to examine the set of predefined operators for suitability. (Enums are also excluded by this section, since neither side is an enum type.) The relevant predefined operators are listed in sections 14.9.1 to 14.9.3, and they are all operators upon primitive numeric types, along with the lifted versions of these operators (note that strings operators are not included here).

  3. Finally, we must perform overload resolution using these operators and the rules in 14.4.2.

Actually performing this resolution would be extremely tedious, but luckily there is a shortcut. Under 14.2.6 there is an informative example given of the results of overload resolution, which states:

...consider the predefined implementations of the binary * operator:

int operator *(int x, int y);
uint operator *(uint x, uint y);
long operator *(long x, long y);
ulong operator *(ulong x, ulong y);
void operator *(long x, ulong y);
void operator *(ulong x, long y);
float operator *(float x, float y);
double operator *(double x, double y);
decimal operator *(decimal x, decimal y);

When overload resolution rules (§14.4.2) are applied to this set of operators, the effect is to select the first of the operators for which implicit conversions exist from the operand types.

Since both sides are null we can immediately throw out all unlifted operators. This leaves us with the lifted numeric operators on all primitive numeric types.

Then, using the previous information, we select the first of the operators for which an implicit conversion exists. Since the null literal is implicitly convertible to a nullable type, and a nullable type exists for int, we select the first operator from the list, which is int? >= int?.

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-04-28 20:56

They seem to be mixing paradigms from C and SQL.

In the context of nullable variables, null == null should really yield false since equality makes no sense if neither value is known, however doing that for C# as a whole would cause issues when comparing references.

查看更多
三岁会撩人
5楼-- · 2019-04-28 21:01

This isn't the "official" answer, it's my best guess. But if you're dealing with nullable ints and comparing them, you most likely always want the comparison to return false if you're dealing with two "int?"s that are null. That way if it returns true, you can be sure you've actually compared two integers, not two null values. It just removes the need for a separate null check.

That said, it is potentially confusing if it's not the behaviour you expect!

查看更多
家丑人穷心不美
6楼-- · 2019-04-28 21:02

The behavior is documented in this page about Nullable Types. It doesn't give a real explanation for why, but my interpretation is the following. > and < have no meaning when with regards to null. null is the absence of a value and thus is only equal to another variable that also has no value. Since there is no meaning for > and <, that is carried over to >= and <=.

查看更多
登录 后发表回答