三元运算符 VB 与 C#:为什么将 Nothing 解析为零?
我只是搬起石头砸自己的脚,想知道是否有真正的原因导致这种情况成为可能。
不管怎样,这个问题可以留下来,以方便未来的足射手。
假设我们在 vb.net 中有一个可为 null 的值:
Dim i as Integer?
我们想根据条件并使用三元运算符为其分配一个值,因为它非常简洁:
i = If(condition(), Nothing, 42)
也就是说,如果条件为 true< /code>,采用可空性,否则采用值。
此时发生枪击事件。没有任何明显的原因,VB 编译器决定 Nothing
和 Integer
的公共基类型是 Integer
,此时它会默默地将语句翻译为:
i = If(condition(), 0, 42)
现在,如果您要在 C# 中执行此操作:
i = (condition()) ? null : 42;
您将立即收到编译器错误,指出
与 int
不能很好地混合。这很棒,因为如果我这次采用 C# 方式,我的脚会更健康。为了编译这个,你必须显式地写:
i = (condition()) ? null : (int?)42;
现在,你可以在VB中做同样的事情并得到你期望的正确的空值:
i = If(condition(), Nothing, CType(42, Integer?))
但这需要首先让你的脚射门。没有编译器错误,也没有警告。这就是Explicit On
和Strict On
。
所以我的问题是,为什么?
我应该将此视为编译器错误吗?
或者有人可以解释为什么编译器会这样?
I just shoot myself in the foot and would like to know whether there were actual reasons to make this situation possible.
And anyway, this question can stay for the convenience of the future foot shooters.
Suppose we have a nullable value in vb.net:
Dim i as Integer?
We want to assign a value to it, basing on a condition, and using a ternary operator, because it's so neat and stuff:
i = If(condition(), Nothing, 42)
That is, if a condition is true
, employ the nullability, otherwise the value.
At which point the shooting occurs. For no apparent reason VB compiler decides that the common base type for Nothing
and Integer
is Integer
, at which point it silently translates the statement to:
i = If(condition(), 0, 42)
Now, if you were to do this in C#:
i = (condition()) ? null : 42;
You would immediately get a compiler error saying that <null>
doesn't mix well with int
. Which is great, as my foot would have been healthier had I went the C# way this time. And for this to compile, you have to explicitly write:
i = (condition()) ? null : (int?)42;
Now, you can do the same in VB and get the correct null-ness you would expect:
i = If(condition(), Nothing, CType(42, Integer?))
But that requires having your foot shot in the first place. There's no compiler error and there's no warning. That's with Explicit On
and Strict On
.
So my question is, why?
Should I take this as a compiler bug?
Or can someone explain why the compiler behaves this way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是因为 VB 的
Nothing
并不直接等同于 C# 的null
。例如,在 C# 中,此代码将无法编译:
但此 VB.Net 代码工作得很好:
VB.Net 的
Nothing
实际上与 C# 的default(T)
更接近。表达。This is because VB's
Nothing
is not a direct equivalent to C#'snull
.For example, in C# this code will not compile:
But this VB.Net code works just fine:
VB.Net's
Nothing
is actually a closer match for C#'sdefault(T)
expression.三元运算符只能返回一种类型。
在 C# 中,它尝试根据
null
和42
选择类型。嗯,null
没有类型,所以它决定三元运算符的返回类型是42
;一个普通的旧int
。然后它会抱怨,因为你不能将 null 作为普通的旧int
返回。当您将 42 强制转换为int?
时,三元运算符将返回int?
,因此null
有效。现在,我不了解VB,但引用MSDN,
对变量不赋值会将其设置为其声明类型的默认值。
其中,由于 VB 确定三元运算符将返回
int
(使用与 C# 相同的过程) ),无
为0
。同样,将42
强制为int?
会将Nothing
转换为int?
的默认值,即null
,如您所料。The ternary operator can only return one type.
In C#, it tries to choose a type based on
null
and42
. Well,null
doesn't have a type, so it decides that the return type of the ternary operator is that of42
; a plain oldint
. Then it complains because you can't return null as a plain oldint
. When you coerce 42 as aint?
, the ternary operator is going to return anint?
, sonull
's valid.Now, I don't know about VB, but quoting from the MSDN,
Assigning Nothing to a variable sets it to the default value for its declared type.
Which, since VB determines that the ternary operator will return an
int
(using the same process C# did),Nothing
is0
. Again, coercing the42
to be anint?
turnsNothing
into the default value ofint?
, which isnull
, as you expected.我认为这与 IF 更多相关,而不是与 Nothing 相关。考虑一下这段代码:
我仍然不知道为什么要这样做,这可能是 VB 团队的问题。我认为这与 Nothing 关键字或 Nullable 无关。
I am thinking this has something more to do with IF than with Nothing. Consider this code:
Why it is doing this I still don't know, probably a question for the VB team. I don't think it has to do with the Nothing keyword or Nullable.
Nothing
和null
不是同一件事......来自 MSDN:还
还记得 int 吗?是可以为 null 的类型,但它仍然是值类型,而不是引用类型。
尝试将其设置为
DbNull.Value
而不是Nothing
...Nothing
andnull
are not the same thing... from the MSDN:Also
Keep in mind that int? is a nullable type, but it's still a value type, not a reference type.
Try setting it to
DbNull.Value
instead ofNothing
...在许多情况下,
Nothing
会转换为默认值。要以与使用null
相同的方式使用Nothing
,您需要将其转换为正确的可空类型。In a number of cases
Nothing
will get converted to the default value. To useNothing
the same way you would usenull
you need to cast it to the correct nullable type.发生这种情况是因为 Integer 不是引用类型。 “Nothing”仅适用于引用类型。对于值类型,赋值 Nothing 会自动转换为默认值,即整数 0 的情况。
This happens because Integer is not a reference type. 'Nothing' is only supposed to work for reference types. For value types assigning Nothing is automatically converted to the default value, which is in the case of an Integer 0.
现在实际上可以通过使用 New Integer 在 VS2015 中(至少)实现这一点?
前任。:
This is actually now possible in VS2015 (at the very least) by using New Integer?
Ex.: