VB.NET:布尔值,来自“无”,有时为“假”,有时为Nullreference-Exception [英] VB.NET: Boolean from `Nothing` sometimes `false`, sometimes Nullreference-Exception
问题描述
来自 C#中的基本布尔逻辑 ,我想知道为什么:
Coming from Basic boolean logic in C#, I was wondering why:
Dim b As Boolean
Dim obj As Object = Nothing
'followig evaluates to False'
b = DirectCast(Nothing, Boolean)
'This throws an "Object reference not set to an instance of an object"-Exception'
b = DirectCast(obj, Boolean)
A CType(obj,Boolean)
的计算结果为 False
(就像 CBool(obj))
一样。我认为这是因为编译器使用了辅助函数,但这不是我的主题。
A CType(obj, Boolean)
would evaluate to False
(just as CBool(obj))
. I think it is because the compiler uses a helper function, but that is not my theme.
为什么强制转换 Nothing
转换为布尔值
的结果为 False
,而强制转换为 Nothing
到 Boolean
会抛出Nullreference-Exception吗?
Why does casting Nothing
to Boolean
evaluates to False
, whereas casting an object that is Nothing
to Boolean
throws an Nullreference-Exception? Does that make sense?
[Option Strict ON]
推荐答案
大概是因为 什么都没有
与C#中的 null
并不完全相同。
Presumably, this is because Nothing
in VB.NET is not exactly the same thing as null
in C#.
对于值类型,没有表示该类型的默认值 。对于
Boolean
,默认值为 False
,因此强制转换成功。
In the case of value types, Nothing
implies the default value of that type. In the case of a Boolean
, the default value is False
, so the cast succeeds.
值类型(例如Integer或结构)与引用类型(例如Form或String)之间的主要区别之一是引用类型支持空值。也就是说,引用类型变量可以包含值Nothing,这意味着该变量实际上并未引用值。相反,值类型变量始终包含一个值。一个整数变量始终包含一个数字,即使该数字为零。如果将值Nothing分配给值类型变量,则只会为值类型变量分配其默认值(对于Integer,该默认值为零)。当前的CLR无法查看Integer变量并确定是否从未分配过值-它包含零的事实并不一定意味着尚未分配值。
– 关于可空类型和变量的真相VB ...
编辑:为进一步说明,第二个示例抛出的原因 NullReferenceException
在运行时是因为CLR试图将 Object
(引用类型)拆箱为布尔值
。当然,这失败了,因为该对象是使用空引用初始化的(将其设置为 Nothing
):
EDIT: For further clarification, the reason the second example throws a NullReferenceException
at run-time is because the CLR is attempting to unbox the Object
(a reference type) to a Boolean
. This fails, of course, because the object was initialized with a null reference (setting it equal to Nothing
):
Dim obj As Object = Nothing
记住,因为我上面已经解释过,关于,VB.NET关键字 Nothing
仍然与C#中的 null
相同。 >引用类型。这就说明了为什么要获取 NullReferenceException
的原因,因为您尝试投射的对象实际上是空引用。它根本不包含任何值,因此无法取消装箱为 Boolean
类型。
Remember that, as I explained above, the VB.NET keyword Nothing
still works the same way as null
in C# when it comes to reference types. That explains why you're getting a NullReferenceException
because the object you're attempting to cast is literally a null reference. It does not contain a value at all, and therefore cannot be unboxed to a Boolean
type.
您不当您尝试将关键字 Nothing
强制转换为布尔值时,不会看到相同的行为,即:
You don't see the same behavior when you try to cast the keyword Nothing
to a Boolean, i.e.:
Dim b As Boolean = DirectCast(Nothing, Boolean)
因为关键字没什么
(这次,在值类型的情况下)仅表示此类型的默认值。对于 Boolean
而言,这是 False
,因此强制转换是合乎逻辑和直接的。
because the keyword Nothing
(this time, in the case of value types) simply means "the default value of this type". In the case of a Boolean
, that's False
, so the cast is logical and straightforward.
这篇关于VB.NET:布尔值,来自“无”,有时为“假”,有时为Nullreference-Exception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!