“foo = Nothing” 和有什么不一样?和“foo 没什么”在 VB.NET 中?
有什么区别
if foo is Nothing Then
doStuff()
End If
在VB.NET中,和
if foo=Nothing Then
doStuff()
End If
更新 我收到以下答复:
foo is Nothing
只是检查foo
是否未分配给任何引用。foo = Nothing
检查foo
持有的引用是否等于nothing
。
运行这三个语句后,
Dim foo as Object
Dim bar as Integer
foo = bar
foo is Nothing
的计算结果为 false,foo = Nothing
的计算结果为 true。
但是,如果 bar
被声明为 Object
并且未初始化,则 foo is Nothing
和 foo = Nothing
两者评价为真!我认为这是因为 Integer
是值类型,而 Object
是引用类型。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这取决于类型。
对于值类型,
Is
不起作用,仅=
和Nothing< /code> 指的是该类型的默认实例(即通过为给定类型
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?
(注意?
)。然而,由于提升运算符所谓的“空传播”,无论foo
的值是什么,这总是返回Nothing
。引用 Visual Basic 10 语言规范 (§1.86.3):<块引用>
如果以太(原文如此!)操作数为
Nothing
,则表达式的结果为Nothing
值,键入为结果类型的可为空版本。因此,如果用户想要将
Nullable
变量与Nothing
进行比较,他们必须再次使用foo Is Nothing
语法,编译器生成特殊代码以使其工作(Visual Basic 10 语言规范的第 1.79.3 节)。向乔纳森·艾伦(Jonathan Allen)致敬,他(正确地)坚持认为我错了;感谢 Jared Parsons 向我传递了 Visual Basic 10 规范的链接。
(上面假设使用了
Option Strict On
,因为您总是应该这样做如果情况并非如此,结果会略有不同,因为调用foo = Nothing
可能会执行后期绑定调用。)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):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 callingfoo = Nothing
may perform a late-bound call.)在 VB 中,如果
foo
尚未初始化,两个语句的计算结果将相同In VB, both statements will evaluate to the same value if
foo
has not been initialised下面是一些用于验证差异的 IL:
VB 代码:
Here's some IL to validate the differences:
VB Code:
foo 是指向内存位置的指针,Nothing 表示“未指向任何内存,因为尚未分配内存”。等于意味着当您比较 2 个值类型时它们具有相同的值。但是您假设 foo 代表一个对象,它始终是一个引用类型,旨在指向内存中的对象。 'is' 用于比较对象类型,并且仅当有两个对象指向相同值时才返回 'true'。
假设您有 clsFoo 和一个公共整数成员变量“x”,并且 foo1 和 foo2 都是 clsFoo,并且 y 和 z 是整数
永远不要忘记打开 option strict。如果失败了,那就是尖叫“请让我的程序糟透了。”
foo is a pointer to a memory location and Nothing means 'not pointing to any memory because no memory has been allocated yet'. Equals means that when you are comparing 2 value types they have the same value. But you're assuming foo represents an object, which is always a reference type that is meant to point to an object in memory. 'is' is for comparing object types and will only return 'true' if you have two objects pointing to the same value.
Say you have clsFoo with one public integer member variable 'x' and foo1 and foo2 are both clsFoo, and y and z are integers
NEVER forget to turn option strict on. To fail this is to scream 'PLEASE make my program SUCK.'
假设:
Foo - 如果 ValueType 则装箱
如果 foo 是 Nothing 那么
if foo=Nothing then
Vb.Net 携带这种情况,如 Obj1 = Obj2。
它不使用 Obj.equals(obj2) !如果 Obj1 没有任何异常,则
此选项使用非常复杂的代码,因为有很多选项取决于所有可能的 foo 定义。
试试这个:
Asume:
Foo - Boxed if ValueType
if foo is Nothing Then
if foo=Nothing Then
Vb.Net carry this case like Obj1 = Obj2.
It do´nt uses Obj.equals(obj2) ! Exception if Obj1 is nothing
This option uses a very complex code as there are a lot options depending all posible foo definitions.
try this:
这取决于 Foo 的类型。
引用类型
值类型
可空值类型
很多人不理解 VB 中的空传播。与 SQL 一样,它使用三值逻辑,因此“a=b”的答案可能是 True、False 或 Null。在
If
语句中,Null 被视为 False。警告你不能只写
If Not(Foo = Nothing) then
,因为“Not (Nothing)”仍然是“Nothing”。It depends on Foo's type.
Reference Types
Value Types
Nullable Value Types
A lot of people don't understand Null Propogation in VB. Like SQL, it uses three-value logic so the answer for "a=b" could be True, False, or Null. In an
If
statement a Null is treated as a False.Warning You can't just write
If Not(Foo = Nothing) Then
because 'Not (Nothing)' is still 'Nothing'.