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

前端 未结 7 605
鱼传尺愫
鱼传尺愫 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:31

    For most methods I assume undefined behavior when called with this==null. That's because most programmers write their code under the assumption that this!=null, which is guaranteed by the C# specification if the calling code is written in C#.

    That's why every sane caller of x.Equals(y) should either know for sure that that x is not null, or add a manual null check.

    In most cases I wouldn't call Equals directly at all, but instead use EqualityComparer.Default.

提交回复
热议问题