Is there a difference between x is null and ReferenceEquals(x, null)?

后端 未结 4 1463
悲哀的现实
悲哀的现实 2020-12-19 13:01

When I write this:

ReferenceEquals(x, null)

Visual studio suggests that the

null check can be simplified.

4条回答
  •  悲哀的现实
    2020-12-19 13:34

    They mean the same in this case, yes. Most would use x == null though.

    I guess ReferenceEquals could be little bit confusing because actually null is a literal that means no reference at all. How can any reference be equal to no reference?

    Note that x is null is only allowed with C#7 and it's pattern matching feature. Normally you use is to check if x is a compatible type but null is not a type. So this is a bit confusing as well.

    That's why i prefer x == null

提交回复
热议问题