7

我有两个问题:

  1. DateDateTime:它们在 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

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

class Program
    {
        static void Main(string[] args)
        {
            DateTime dt = null;//compile time error            
        }
    }
4

3 回答 3

14

Nothing在 VB.NET 中与null在 C# 中不同。它还具有defaultC# 中的功能,当您在System.DateTime.

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

Dim dt As Date = Nothing 

实际上是一样的

Dim dt = Date.MinValue

或(在 C# 中)

DateTime dt = default(DateTime);
于 2013-01-11T15:38:29.160 回答
3

在c#中你可以使用default关键字

DateTime dt = default(DateTime);

Date并且DateTime在 VB.NET 中是相同的。Date只是别名_DateTime

于 2013-01-11T15:38:38.157 回答
3

在 vb.net中Date只是.DateTime

VB 中存在的一些别名用于遗留目的,以帮助从 vb6 应用程序进行转换。

于 2013-01-11T15:40:01.090 回答