1

This statement seems to work, but I'm not sure exactly how

int id = 0; // non-nullable ("primative"/value-type/struct)

_list?.Find(t => { return t?.Id == id; })?.DeptId ?? 0;

If t is null, then t?.Id is null, correct?

Then how is this legal:

null == id

Update (5/8/18)

The statement in visual studio

if (null == 0) {}

Gets highlighted green and upon inspection with a mouse over states

"The result of the expression is always 'false' since a value of type 'int' is never equal to 'null' of type 'int?'


Update (5/24/18)

Nullable<T> Struct

Fundamental Properties

The two fundamental members of the Nullable structure are the HasValue and Value properties. If the HasValue property for a Nullable object is true, the value of the object can be accessed with the Value property. If the HasValue property is false, the value of the object is undefined and an attempt to access the Value property throws an InvalidOperationException.

Boxing and Unboxing

When a nullable type is boxed, the common language runtime automatically boxes the underlying value of the Nullable object, not the Nullable object itself. That is, if the HasValue property is true, the contents of the Value property is boxed. When the underlying value of a nullable type is unboxed, the common language runtime creates a new Nullable structure initialized to the underlying value.

If the HasValue property of a nullable type is false, the result of a boxing operation is null. Consequently, if a boxed nullable type is passed to a method that expects an object argument, that method must be prepared to handle the case where the argument is null. When null is unboxed into a nullable type, the common language runtime creates a new Nullable structure and initializes its HasValue property to false.

ref: https://learn.microsoft.com/en-us/dotnet/api/system.nullable-1?view=netframework-4.7.2

samus
  • 6,102
  • 6
  • 31
  • 69
  • It won't even do `t.id==Id` if t is null. It will simply return false. And BTW t.id will not be null ever as it is int not `int?` – Chetan Mar 08 '18 at 15:51
  • `t?.Id` is of type `int?` (i.e. `Nullable`), to which you *can* compare an `int`. – Iridium Mar 08 '18 at 15:51
  • I guess because `null` on an integer evaluates to it's default value, as per any struct, in this case `0`. – Ian H. Mar 08 '18 at 15:51
  • @MongZhu `List` where `Id` is a property of `T` (`public int Id {get;set;}`), so is nullable. – samus Mar 08 '18 at 16:13
  • @samusarin sorry for that stupid question. My attention span has become too short for SO XD I guess I need to call it a day ;) – Mong Zhu Mar 08 '18 at 16:24
  • 1
    @MongZhu Heh, no worries I barely have capacity for even videogames after work these days. I knew what ya meant though... I am actually wrong, the `Id` property still returns a "primitive" (value-type) `int`, so it is not nullable (I conflated things). However, the overloaded/lifted `==` operator still takes care of casting/wrapping/boxing it into one. – samus Mar 08 '18 at 16:29
  • 1
    @IanH. No, that's not true at all. `null == 0` is false. `null` is it's own value, equal to `null` and not equal to any other value. – Servy Mar 08 '18 at 20:07
  • @ChetanRanpariya I'm sorry I don't follow you? – samus May 24 '18 at 19:12
  • `t?.Id == id` says that compare `t.Id` and `id` only if `t ` is not null and return true or false based on the comparison result. If `t` is null it will not do any comparison and simply return false. – Chetan May 24 '18 at 19:30
  • https://dotnetfiddle.net/v9k5kh – Chetan May 24 '18 at 19:44

1 Answers1

3

From the C# 5.0 spec (Section 7.3.7 Lifted 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.

Thus, the lifted operator int? == int? is used. Since the left-hand side is null and the right-hand side isn't, the operator returns false.

Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519