vb.net – ‘?’字符不能在这里使用

有了这些变量:

Dim d1 As Date? = Nothing
Dim d2 As DateTime? = Nothing
Dim i1 As Integer? = Nothing
Dim i2 As Int32? = Nothing

为什么我被允许这样做?:

Dim flag1 As Boolean = Date?.Equals(d1, d2)
Dim flag2 As Boolean = Integer?.Equals(i1, i2)

……但不允许这样做?:

Dim flag3 As Boolean = DateTime?.Equals(d2, d1)
Dim flag4 As Boolean = Int32?.Equals(i2, i1)

最后一个代码将失败并显示错误:

The ‘?’ character cannot be used here.

VB.NET开发人员不应该使用C#关键字(宗教,你知道).说真的,我同意@Konrad看起来像编译错误.如果您有其他VS,请尝试在那里,我只在VS 2010 SP1中尝试过,因为这就是我的工作.如果您注意到一致性,也许您应该在 connect上报告它.作为一种解决方法,您可以尝试这样做:

Dim flag3 As Boolean = d1.Equals(d1)
Dim flag4 As Boolean = i2.Equals(i1)

或这个:

Dim flag3 As Boolean = Nullable(Of DateTime).Equals(d1, d2)
Dim flag4 As Boolean = Nullable(Of Int32).Equals(i1, i2)

我个人更喜欢我的代码中的最后一个选项,明确地说Nullable(Of T),因为VB语言应该是冗长的,更像英语,即没有:? || &安培;&安培;构造(没有冒犯,C#开发).

相关文章
相关标签/搜索