!(x is null) 和 x is object 之间有区别吗
Is there a difference between !(x is null) and x is object
!(x is null)
和x is object
有区别吗?
在某些情况下他们会 return 不同的结果吗?
没有明显的区别。它们都编译为相同的 CIL。
自己检查一下here
注意:如您所见here[,即使是可空类型也是如此=41=]
来自Standard ECMA-334 C# Language Specification
12.11.11 The is operator
The is operator is used to check if the run-time type of an object is
compatible with a given type. The check is performed at runtime. The
result of the operation E is T, where E is an expression and T is a
type other than dynamic, is a Boolean value indicating whether E is
non-null and can successfully be converted to type T by a reference
conversion, a boxing conversion, an unboxing conversion, a wrapping
conversion, or an unwrapping conversion.
更新
完全简洁并如Alexei Levenkov
所指出的那样
!(1 is null)
与 1 is object
显示出一些差异,前者不会编译,因为不是 nullable
!(x is null)
和x is object
有区别吗?
在某些情况下他们会 return 不同的结果吗?
没有明显的区别。它们都编译为相同的 CIL。
自己检查一下here
注意:如您所见here[,即使是可空类型也是如此=41=]
来自Standard ECMA-334 C# Language Specification
12.11.11 The is operator
The is operator is used to check if the run-time type of an object is compatible with a given type. The check is performed at runtime. The result of the operation E is T, where E is an expression and T is a type other than dynamic, is a Boolean value indicating whether E is non-null and can successfully be converted to type T by a reference conversion, a boxing conversion, an unboxing conversion, a wrapping conversion, or an unwrapping conversion.
更新
完全简洁并如Alexei Levenkov
所指出的那样!(1 is null)
与 1 is object
显示出一些差异,前者不会编译,因为不是 nullable