有理由选择其中一个而不是另一个吗?

DateTime myDate = new DateTime();

or

DateTime myDate = default(DateTime);

它们都是相等的1/1/0001 12:00:00 AM。


当前回答

如果要为方法中的DateTime参数使用默认值,则只能使用default(DateTime)。

下面的行不能编译:

    private void MyMethod(DateTime syncedTime = DateTime.MinValue)

这一行将编译:

    private void MyMethod(DateTime syncedTime = default(DateTime))

其他回答

答案是否定的。请记住,在这两种情况下,mdDate。Kind = DateTimeKind.Unspecified。

因此,最好做到以下几点:

DateTime myDate = new DateTime(1, 1, 1, 0, 0, 0, DateTimeKind.Utc);

替换。Kind属性是只读的,因此在调用构造函数后不能更改它。

不,它们是一样的。

default(),对于任何值类型(DateTime是一种值类型)都会调用无参数构造函数。

最简单的理解方法是,DateTime是一个结构体。当你初始化一个结构时,它初始化为它的最小值:DateTime。最小值

因此,default(DateTime)和new DateTime()和DateTime之间没有区别。最小值

如果要为方法中的DateTime参数使用默认值,则只能使用default(DateTime)。

下面的行不能编译:

    private void MyMethod(DateTime syncedTime = DateTime.MinValue)

这一行将编译:

    private void MyMethod(DateTime syncedTime = default(DateTime))