VB.NET和C#中的DateTime

Bri*_*rij 6 .net c# vb.net datetime value-type

我有两个问题:

  1. Date并且DateTime:它们在VB中是不同的还是相同的?

  2. 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
Run Code Online (Sandbox Code Playgroud)

--- C#.NET -----

class Program
    {
        static void Main(string[] args)
        {
            DateTime dt = null;//compile time error            
        }
    }
Run Code Online (Sandbox Code Playgroud)

Tim*_*ter 13

Nothing在VB.NET中与nullC#中的不一样.它还具有defaultC#中的功能,当你在类似的结构上使用它时会发生这种情况System.DateTime.

所以两者,DateDateTime引用相同的结构System.DateTime

Dim dt As Date = Nothing 
Run Code Online (Sandbox Code Playgroud)

实际上是一样的

Dim dt = Date.MinValue
Run Code Online (Sandbox Code Playgroud)

或(在C#中)

DateTime dt = default(DateTime);
Run Code Online (Sandbox Code Playgroud)