2

The C# 5.0 spec reads in chapter 7.1.3

https://msdn.microsoft.com/en-us/library/ms228593.aspx

The lifted operator produces the value false if one or both operands are null.

However testing and also this MSDN link

http://msdn.microsoft.com/en-us/library/2cf62fcy(v=vs.100).aspx

int? num1 = 10;
int? num2 = null;

// Change the value of num1, so that both num1 and num2 are null.
num1 = null;
if (num1 == num2)
{
  // The equality comparison returns true when both operands are null.
  Console.WriteLine("num1 == num2 returns true when the value of each is null");
}

/* Output:
 * num1 == num2 returns true when the value of each is null
 */

shows that comparing two nullable values that are both null returns true.

It makes sense but it contradicts the sentence from the spec, does it not?

Dee J. Doena
  • 1,631
  • 3
  • 16
  • 26
  • I found this in chapter 7.3.7. Notice this is for `binary operators`. The very next section talks about `equality operators`, which includes `==`. – Gavin Oct 05 '16 at 12:33
  • 1
    Possible duplicate of [Null-conditional operator evaluates to bool not to bool? as expected](http://stackoverflow.com/questions/37277102/null-conditional-operator-evaluates-to-bool-not-to-bool-as-expected) – Thomas Ayoub Oct 05 '16 at 12:35

2 Answers2

5

Dont mix, this is about different types of operators.

• 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.

• 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.

tym32167
  • 4,741
  • 2
  • 28
  • 32
4

Spec later says that

• 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.

Evk
  • 98,527
  • 8
  • 141
  • 191