VB.NET: Boolean from `Nothing` sometimes `false`, sometimes Nullreference-Exception(VB.NET:来自“Nothing的布尔值,有时为“false,有时为 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
转换为 Boolean
的结果为 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]
推荐答案
大概是因为Nothing
在 VB.NET 中与 null
在 C# 中并不完全相同.
Presumably, this is because Nothing
in VB.NET is not exactly the same thing as null
in C#.
对于值类型,Nothing
暗示该类型的默认值.在 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,这意味着该变量实际上并不引用一个值.相反,值类型变量总是包含一个值.Integer 变量始终包含一个数字,即使该数字为零.如果将值 Nothing 分配给值类型变量,则值类型变量只会被分配其默认值(在 Integer 的情况下,该默认值为零).当前的 CLR 无法查看 Integer 变量并确定它是否从未被赋值——它包含零的事实并不一定意味着它没有被赋值.
–可空类型和VB的真相...
One of the primary differences between value types such as Integer or structures and reference types such as Form or String is that reference types support a null value. That is to say, a reference type variable can contain the value Nothing, which means that the variable doesn't actually refer to a value. In contrast, a value type variable always contains a value. An Integer variable always contains a number, even if that number is zero. If you assign the value Nothing to a value type variable, the value type variable just gets assigned its default value (in the case of Integer, that default value is zero). There is no way in the current CLR to look at an Integer variable and determine whether it has never been assigned a value - the fact that it contains zero doesn't necessarily mean that it hasn't been assigned a value.
–The Truth about Nullable Types and VB...
编辑:为了进一步说明,第二个示例在运行时抛出 NullReferenceException
的原因是因为 CLR 试图拆箱 Object
(引用类型)到 Boolean
.当然,这会失败,因为该对象是使用空引用初始化的(将其设置为 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
相同/em>.这就解释了为什么你得到一个 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)
因为关键字Nothing
(这次是在值类型的情况下)仅仅意味着这种类型的默认值".在 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:来自“Nothing"的布尔值,有时为“false",有时为 Nullreference-Exception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:VB.NET:来自“Nothing"的布尔值,有时为“false",有时为 Nullreference-Exception
基础教程推荐
- SSE 浮点算术是否可重现? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- c# Math.Sqrt 实现 2022-01-01