为什么DateTime.MinValue不能用作C#中的可选参数

发布于 2024-12-28 15:13:28 字数 714 浏览 2 评论 0原文

我正在编写一个方法,它将 DateTime 值作为其参数之一。我认为它是可选参数,因此我尝试将 DateTime.MinValue 设置为默认参数。

private void test(string something, DateTime testVar = DateTime.MinValue) {

}

然而这会产生一个错误:

“testVar”的默认参数值必须是编译时常量。

使用这段代码似乎工作得很好。

private void test(string something, DateTime testVar = new DateTime()) {

}

有人建议我使用 DateTime.MinValue 而不是 new DateTime()< /a> 因为它是自我记录的。由于 new DateTime() 基本上是相同的事情,为什么不能使用 DateTime.MinValue ?如果我用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

情栀口红 2025-01-04 15:13:28

DateTime.MinValue 定义为:

public static readonly DateTime MinValue

const 不同。由于readonly值不是编译时常量(即该值在编译时计算),因此不能使用它。

使用 new DateTime() 之所以有效,是因为该表达式在编译时是已知的。这与编写 default(DateTime) 相同。例如,以下表达式中的 result == true

var result = new DateTime() == default(DateTime);

DateTime.MinValue is defined as:

public static readonly DateTime MinValue

Which is not the same as const. Since a readonly 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 writing default(DateTime). For example, result == true in the following expression:

var result = new DateTime() == default(DateTime);
〃安静 2025-01-04 15:13:28

其他答案涉及为什么不能使用 DateTime.MinValue,它不是合法的编译时间常量。它是一个静态只读字段,就使用而言很可能是恒定的,但不是合法恒定的,也不符合可使用的规则作为默认参数。至于为什么可以使用new DateTime(),请参见C# 4.0 语言规范。相关位:

默认参数中的表达式必须是以下之一:

· 常量表达式

· new S() 形式的表达式,其中 S 是值类型

· default(S) 形式的表达式,其中 S 是值类型

这会导致零初始化实例,基本上是全零的位模式。 (参见:第 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 why new DateTime() can be used, see section 10.6.1 of the C# 4.0 Language Specification. Relevant bits:

The expression in a default-argument must be one of the following:

· a constant-expression

· an expression of the form new S() where S is a value type

· an expression of the form default(S) where S is a value type

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.

能怎样 2025-01-04 15:13:28

DateTime.MinValue 是 只读,并且根据 MSDN,只读值不是编译时常量:

readonly 关键字与 const 关键字不同。 const 字段只能在字段声明时初始化。只读字段可以在声明处或构造函数中初始化。因此,只读字段可以具有不同的值,具体取决于所使用的构造函数。此外,虽然 const 字段是编译时常量,但 readonly 字段可用于运行时常量

DateTime.MinValue is readonly, and as per MSDN, readonly values are not compile-time constants:

The readonly keyword is different from the const keyword. A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used. Also, while a const field is a compile-time constant, the readonly field can be used for runtime constants

十秒萌定你 2025-01-04 15:13:28

DateTime.MinValue(和DateTime.MaxValue)是public static readonly成员,而不是编译时常量。

为什么不使用可为 null 的 DateTime (DateTime?),而不是使用 DateTime.MinValue 作为默认值。这使您的意图比默认为日期时间的最低可能值更加清晰。

像这样的:

private void test(string something, DateTime? testVar = null )
{
  if ( testVar.HasValue )
  {
     DoSomethingUsefulWithTimestamp( something , testVar.Value ) ;
  }
  else
  {
     DoSomethingElseWithoutTimestamp( something ) ;
  }
  return ;
}

private void DoSomethingUsefulWithTimestamp( string something , DateTime dt )
{
  ... // something useful
}
private void DoSomethingElseWithoutTimestamp( string something )
{
  ... // something useful
}

或者,在方法主体中设置默认值:

private void test(string something, DateTime? testVar = null )
{
  DateTime dtParameter = testVar ?? DateTime.MinValue ;

  DoSomethingUsefulWithTimestamp( something , dtParameter ) ;

}

DateTime.MinValue (and DateTime.MaxValue) are public 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:

private void test(string something, DateTime? testVar = null )
{
  if ( testVar.HasValue )
  {
     DoSomethingUsefulWithTimestamp( something , testVar.Value ) ;
  }
  else
  {
     DoSomethingElseWithoutTimestamp( something ) ;
  }
  return ;
}

private void DoSomethingUsefulWithTimestamp( string something , DateTime dt )
{
  ... // something useful
}
private void DoSomethingElseWithoutTimestamp( string something )
{
  ... // something useful
}

Alternatively, set your default in your method body:

private void test(string something, DateTime? testVar = null )
{
  DateTime dtParameter = testVar ?? DateTime.MinValue ;

  DoSomethingUsefulWithTimestamp( something , dtParameter ) ;

}
如梦初醒的夏天 2025-01-04 15:13:28

另一种替代方法是有 2 个方法重载:

  • 一个采用 DateTime 参数
  • 一个不采用 DateTime 参数

这样做的优点是您不必检查参数是否为 null,并且很清楚什么是空值你的意图是。在内部,方法 1 可以向数据库添加 null。

Another alternative would be to have 2 method overloads:

  • One that takes a DateTime parameter
  • One that doesn't take the DateTime parameter

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.

呆头 2025-01-04 15:13:28

根据我所知,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()

只涨不跌 2025-01-04 15:13:28

使用这个语句

private void test(string something, DateTime testVar = new DateTime()) {
    if ( testVar != new DateTime() )
    {
        DoSomethingUsefulWithTimestamp( something , testVar.Value ) ;
    }
    else
    {
        DoSomethingElseWithoutTimestamp( something ) ;
    }
}

应该会效果更好。令人遗憾的是 null 不起作用,因为它更有意义。

Use this statement

private void test(string something, DateTime testVar = new DateTime()) {
    if ( testVar != new DateTime() )
    {
        DoSomethingUsefulWithTimestamp( something , testVar.Value ) ;
    }
    else
    {
        DoSomethingElseWithoutTimestamp( something ) ;
    }
}

It should work much better. It is a bummer that null doesn't work, because it would make more sense.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文