为什么DateTime.MinValue不能用作C#中的可选参数
我正在编写一个方法,它将 DateTime 值作为其参数之一。我认为它是可选参数,因此我尝试将 DateTime.MinValue
设置为默认参数。
private void test(string something, DateTime testVar = DateTime.MinValue) {
}
然而这会产生一个错误:
“testVar”的默认参数值必须是编译时常量。
使用这段代码似乎工作得很好。
private void test(string something, DateTime testVar = new DateTime()) {
}
I was writing a method which takes DateTime
value as one of it's parameters. I decided that it's optional parameter so I went ahead and tried to make DateTime.MinValue
as default.
private void test(string something, DateTime testVar = DateTime.MinValue) {
}
However this gives an error that:
Default parameter value for 'testVar' must be a compile-time constant.
Using this code seems to work just fine.
private void test(string something, DateTime testVar = new DateTime()) {
}
I was given advice to use DateTime.MinValue instead of new DateTime() as it's self-documenting. Since new DateTime()
is basically the same thing why DateTime.MinValue
can't be used? Also will there be any potential problem if I leave it with new DateTime()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
DateTime.MinValue
定义为:与
const
不同。由于readonly
值不是编译时常量(即该值在编译时不计算),因此不能使用它。使用
new DateTime()
之所以有效,是因为该表达式在编译时是已知的。这与编写default(DateTime)
相同。例如,以下表达式中的result == true
:DateTime.MinValue
is defined as:Which is not the same as
const
. Since areadonly
value is not a compile-time constant (i.e. the value is not evaluated at compile-time), it can't be used.The reason that using
new DateTime()
works is because that expression is known at compile-time. It's the same as writingdefault(DateTime)
. For example,result == true
in the following expression:其他答案涉及为什么不能使用 DateTime.MinValue,它不是合法的编译时间常量。它是一个
静态只读
字段,就使用而言很可能是恒定的,但不是合法恒定的,也不符合可使用的规则作为默认参数。至于为什么可以使用new DateTime()
,请参见C# 4.0 语言规范。相关位:这会导致零初始化实例,基本上是全零的位模式。 (参见:第 4.1.2 节)
但是,在这种情况下,我仍然建议使用
DateTime? value = null
作为参数和默认参数,特别是当它表示数据库中可为空的日期时。MinValue
并不是缺少值。null
是。Other answers touch upon why DateTime.MinValue cannot be used, it is not a legal compile time constant. It is a
static readonly
field, which might very well be constant as far as usage goes, but is not legally constant, nor does it fit the rules for what can be used as a default argument. As for whynew DateTime()
can be used, see section 10.6.1 of the C# 4.0 Language Specification. Relevant bits:These result in a zero-initialized instance, basically a bit pattern of all zeros. (See: Section 4.1.2)
However, in this case, I still recommend using a
DateTime? value = null
as the parameter and default argument, particularly when it's representing a nullable date in a database.MinValue
is not the absence of a value.null
is.DateTime.MinValue 是 只读,并且根据 MSDN,只读值不是编译时常量:
DateTime.MinValue is readonly, and as per MSDN, readonly values are not compile-time constants:
DateTime.MinValue
(和DateTime.MaxValue
)是public static readonly
成员,而不是编译时常量。为什么不使用可为 null 的 DateTime (
DateTime?
),而不是使用DateTime.MinValue
作为默认值。这使您的意图比默认为日期时间的最低可能值更加清晰。像这样的:
或者,在方法主体中设置默认值:
DateTime.MinValue
(andDateTime.MaxValue
) arepublic static readonly
members rather than compile-time constants.Rather than use
DateTime.MinValue
as your default, why not use a nullable DateTime (DateTime?
). That makes your intent rather more clear than defaulting to the lowest possible value of datetime.Something like this:
Alternatively, set your default in your method body:
另一种替代方法是有 2 个方法重载:
这样做的优点是您不必检查参数是否为 null,并且很清楚什么是空值你的意图是。在内部,方法 1 可以向数据库添加 null。
Another alternative would be to have 2 method overloads:
The advantage of this is that you wouldn't have to check if the parameter is null, and it would be clear what your intention is. Internally, method 1 can add a null to the database.
根据我所知,DateTime 的默认值是 DateTime.MinValue 那么为什么不只使用 new DateTime()
based on what I am aware of Default value for DateTime is DateTime.MinValue so why not just use new DateTime()
使用这个语句
应该会效果更好。令人遗憾的是 null 不起作用,因为它更有意义。
Use this statement
It should work much better. It is a bummer that null doesn't work, because it would make more sense.