'foo = Nothing' 和有什么不一样和 'foo is nothing'在 VB.NET 中? [英] What is the difference between 'foo = Nothing' and 'foo is Nothing' in VB.NET?

查看:40
本文介绍了'foo = Nothing' 和有什么不一样和 'foo is nothing'在 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 Nothingfoo = Nothing 都评估为真!我认为这是因为 Integer 是值类型而 Object 是引用类型.

解决方案

这取决于类型.

  • 对于值类型Is不起作用,只有=Nothing 指的是该类型的默认实例(即您通过为给定类型 T 调用 New T() 获得的实例).>

  • 对于引用类型Is 执行引用比较(与object.ReferenceEquals(a, Nothing) 相同).a = Nothing 通常不起作用除非 Operator = 已为该类明确定义.

    此外,如果 Operator = 已正确实现,则 foo = Nothingfoo 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 if foo is not assigned to any reference. foo = Nothing checks if the reference held by foo is equal to nothing.

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 =, and Nothing refers to the default instance of that type (i.e. the instance that you get by calling New T() for a given type T).

  • For reference types, Is performs a reference comparison (identical to object.ReferenceEquals(a, Nothing)). a = Nothing usually does not work, unless Operator = has explicitly been defined for that class.

    If, furthermore, Operator = has been implemented correctly, then foo = Nothing and foo Is Nothing should yield the same result (but the same isn’t true for any other value instead of Nothing) but foo Is Nothing will be more efficient since it’s a compiler intrinsic while Operator = 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 two Nullables is thus not Boolean but Boolean? (note the ?). However, because of so-called "null propagation" for lifted operators, this will always return Nothing, no matter the value of foo. Quoting the Visual Basic 10 language specification (§1.86.3):

    If ether (sic!) operand is Nothing, the result of the expression is a value of Nothing typed as the nullable version of the result type.

    So if the users want to compare a Nullable variable to Nothing, they must use the foo 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.)

这篇关于&amp;#39;foo = Nothing&amp;#39; 和有什么不一样和 &amp;#39;foo is nothing&amp;#39;在 VB.NET 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆