【发布时间】:2019-08-23 13:40:54
【问题描述】:
当我尝试在 vb.net 中使用三元表达式为变量分配空值时,我感到很惊讶。当我通过三元表达式赋值时,它没有按预期工作。
Dim i As Integer? = Nothing
Dim j As Integer? = Nothing
i = If(True, j, 1)
j = If(True, Nothing, 1)
执行此代码后:i 什么都不是,j 变为 0(零)。为什么? 解释是什么? 为什么我不能直接分配 Nothing (Null) 值?
【问题讨论】:
-
好地方。请参阅dotnetfiddle.net/vHDdkk,取自建议副本中的stackoverflow.com/a/17378158/1086121。
-
大卫建议的重复答案appears to be in this answer:您必须将
Integer? j显式转换为Integer?,否则其值Nothing将自动转换为默认值@987654328 @. -
为了得到你期望的结果,将 j = 更改为 j = If(True, New Integer?, 1)
标签: vb.net