有理由选择其中一个而不是另一个吗?
DateTime myDate = new DateTime();
或
DateTime myDate = default(DateTime);
它们都是相等的1/1/0001 12:00:00 AM
。
发布于 2012-12-20 01:20:46
不,它们是一样的。
对于任何值类型(DateTime
是值类型),default()
将始终调用无参数构造函数。
发布于 2015-11-17 16:28:36
如果要对方法中的DateTime参数使用默认值,则只能使用默认值(DateTime)。
下面的代码行不会编译:
private void MyMethod(DateTime syncedTime = DateTime.MinValue)
下面这一行将编译:
private void MyMethod(DateTime syncedTime = default(DateTime))
发布于 2014-06-30 01:05:21
答案是否定的。请记住,在这两种情况下,mdDate.Kind = DateTimeKind.Unspecified
。
因此,执行以下操作可能会更好:
DateTime myDate = new DateTime(1, 1, 1, 0, 0, 0, DateTimeKind.Utc);
myDate.Kind
属性是只读的,因此在调用构造函数后无法更改。
https://stackoverflow.com/questions/13957701
复制相似问题