gpt4 book ai didi

c# - !(x is null) 和 x is object 之间有区别吗

转载 作者:行者123 更新时间:2023-12-03 04:19:13 26 4
gpt4 key购买 nike

!(x is null)x is object 之间有区别吗?

是否存在返回不同结果的情况?

最佳答案

没有明显差异。它们都编译为相同的CIL

自己检查一下here

注意:即使对于可空类型也是如此,如您所见 here


来自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 显示出一些差异,前者由于不可为空而无法编译可空

关于c# - !(x is null) 和 x is object 之间有区别吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58495906/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com