[英]DateTime in VB.NET and C#
我有兩個問題:
Date
和DateTime
:它們在VB中是不同的還是相同的?
可以在VB中為DateTime
分配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
}
}
Nothing
在VB.NET是不一樣的null
在C#。 它還具有C#中的default
功能,這就是在System.DateTime
等結構上使用它時會發生的情況。
因此, Date
和DateTime
引用相同的結構System.DateTime
和
Dim dt As Date = Nothing
實際上是一樣的
Dim dt = Date.MinValue
或(在C#中)
DateTime dt = default(DateTime);
在vb.net中, Date
只是DateTime
的別名 。
VB中存在的一些別名用於遺留目的,以幫助從vb6應用程序進行轉換。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.