【发布时间】:2013-01-11 15:36:25
【问题描述】:
我有两个问题:
Date和DateTime: 它们在 VB 中是不同还是相同?DateTime在 VB 中可以赋值为 Nothing,而在 C# 中不能赋值为 null。作为一个结构,它不能为空。那么为什么它在 VB 中被允许呢?
---VB.NET-----
Module Module1
Sub Main()
Dim d As Date = Nothing
Dim dt As DateTime = Nothing
d = CType(MyDate, DateTime)
End Sub
Public ReadOnly Property MyDate As DateTime
Get
Return Nothing
End Get
End Property
End Module
---C#.NET-----
class Program
{
static void Main(string[] args)
{
DateTime dt = null;//compile time error
}
}
【问题讨论】:
-
@musefan: 好的,但是在 VB.NET 中你可以写
Dim dt As Date? = Nothing。那么有什么区别呢? -
@TimSchmelter:非常恰当地命名为
Dim就是区别。我知道我们很酷的孩子有永远懒惰的“var”,但我对 VB 的盲目仇恨不需要有理由支持,我只需要确保我尽我所能,并在机会出现时发出正确的声音。 .. #CutForVB
标签: c# .net vb.net datetime value-type