'foo = Nothing' 和有什么不一样和 'foo is nothing'在 VB.NET 中? [英] What is the difference between 'foo = Nothing' and 'foo is Nothing' in VB.NET?
问题描述
在VB.NET中,有什么区别
if foo is Nothing Then做东西()万一
和
if foo=Nothing Then做东西()万一
更新我收到以下答复:
<块引用>foo is Nothing
只是检查 foo
是否没有分配给任何引用.foo = Nothing
检查 foo
持有的引用是否等于 nothing
.
运行三个语句后,
将 foo 变暗为对象Dim bar 作为整数foo = 酒吧
foo is Nothing
计算结果为 false,foo = Nothing
计算结果为 true.
然而,如果 bar
被声明为一个 Object
并且没有被初始化,那么 foo is Nothing
和 foo = Nothing代码> 都评估为真!我认为这是因为
Integer
是值类型而 Object
是引用类型.
这取决于类型.
对于值类型,
Is
不起作用,只有=
和Nothing
指的是该类型的默认实例(即您通过为给定类型T
调用New T()
获得的实例).>对于引用类型,
Is
执行引用比较(与object.ReferenceEquals(a, Nothing)
相同).a = Nothing
通常不起作用,除非Operator =
已为该类明确定义.此外,如果
Operator =
已正确实现,则foo = Nothing
和foo Is Nothing
应该产生相同的结果(但对于任何其他值而不是Nothing
) 而言,情况并非如此,但foo Is Nothing
会更高效,因为它是编译器固有的,而Operator =
将调用一个方法.对于可空值类型(即
<块引用>Nullable(Of T)
的实例),适用特殊规则:与所有其他运算符一样,=代码> 解除(注意那篇博文中的错误……)由编译器转为底层类型.因此,比较两个
Nullable
的结果不是Boolean
而是Boolean?
(注意?
).但是,由于提升运算符的所谓空传播",这将总是返回Nothing
,无论foo
的值如何.引用 Visual Basic 10 语言规范(第 1.86.3 节):如果 ether (sic!) 操作数是
Nothing
,则表达式的结果是Nothing
的值,类型为结果类型的可为空版本.因此,如果用户想将
Nullable
变量与Nothing
进行比较,他们必须再次使用foo Is Nothing
语法,编译器生成特殊代码以使其工作(Visual Basic 10 语言规范的第 1.79.3 节).向乔纳森艾伦致敬,因为他(正确地)坚持认为我错了;感谢 Jared Parsons 向我传递了一个指向 Visual Basic 10 规范的链接.
(以上假设使用了 Option Strict On
,因为您总是应该这样做.如果不是这种情况,由于调用 foo = Nothing
可能会执行后期绑定调用.)
In VB.NET, what is the difference between
if foo is Nothing Then
doStuff()
End If
and
if foo=Nothing Then
doStuff()
End If
Update I received the following answer:
foo is Nothing
simply checks iffoo
is not assigned to any reference.foo = Nothing
checks if the reference held byfoo
is equal tonothing
.
After running the three statements,
Dim foo as Object
Dim bar as Integer
foo = bar
foo is Nothing
evaluates to false and foo = Nothing
evaluates to true.
However, if bar
is declared as an Object
and not initialized, then foo is Nothing
and foo = Nothing
both evaluate to true! I think this is because Integer
is a value type and Object
is a reference type.
It depends on the type.
For value types,
Is
doesn’t work, only=
, andNothing
refers to the default instance of that type (i.e. the instance that you get by callingNew T()
for a given typeT
).For reference types,
Is
performs a reference comparison (identical toobject.ReferenceEquals(a, Nothing)
).a = Nothing
usually does not work, unlessOperator =
has explicitly been defined for that class.If, furthermore,
Operator =
has been implemented correctly, thenfoo = Nothing
andfoo Is Nothing
should yield the same result (but the same isn’t true for any other value instead ofNothing
) butfoo Is Nothing
will be more efficient since it’s a compiler intrinsic whileOperator =
will call a method.For nullable value types (i.e. instances of
Nullable(Of T)
), special rules apply: like all other operators,=
is lifted (notice the error in that blog post …) by the compiler to the underlying type. The result of comparing twoNullable
s is thus notBoolean
butBoolean?
(note the?
). However, because of so-called "null propagation" for lifted operators, this will always returnNothing
, no matter the value offoo
. Quoting the Visual Basic 10 language specification (§1.86.3):If ether (sic!) operand is
Nothing
, the result of the expression is a value ofNothing
typed as the nullable version of the result type.So if the users want to compare a
Nullable
variable toNothing
, they must use thefoo Is Nothing
syntax for which, once again, the compiler generates special code to make it work (§1.79.3 of the Visual Basic 10 language specification). Hat tip to Jonathan Allen for (correctly) persisting that I was wrong; hat tip to Jared Parsons for passing me a link to the Visual Basic 10 specification.
(The above assumes that Option Strict On
is used, as you always should. In case that isn’t the case, the results will differ slightly since calling foo = Nothing
may perform a late-bound call.)
这篇关于&#39;foo = Nothing&#39; 和有什么不一样和 &#39;foo is nothing&#39;在 VB.NET 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!