Result of calling IEquatable.Equals(T obj) when this == null and obj == null?

前端 未结 7 531
鱼传尺愫
鱼传尺愫 2021-01-12 07:05

What should IEquatable.Equals(T obj) do when this == null and obj == null?

1) This code is generated

7条回答
  •  天涯浪人
    2021-01-12 07:44

    leppie is right. Just to elaborate on his answer (and confirm his suspicion that F# doesn't guarantee this != null): discriminated unions may be marked with the attribute [] allowing cases to be represented by the value null. Option<'T> is such a type. The None case is represented by null at run-time. (None : option).Equals(None) is syntactically valid. Here's a fun example:

    []
    type Maybe<'T> =
      | Just of 'T
      | Nothing
      []
      member this.ThisIsNull() = match this with Nothing -> true | _ -> false
    

    Decompiling ThisIsNull with Reflector shows

    public bool ThisIsNull()
    {
        return (this == null);
    }
    

    And the result:

    Nothing.ThisIsNull() //true
    

提交回复
热议问题