DateTime可选参数中的默认值应该是多少?

时间:2023-02-01 10:50:56

Normally, if I have a nullable type for an optional parameter, I would put null as the default value. This way I know that if the value is null, the caller doesn't want to specify any value for that one.

通常,如果我有一个可选参数的可空类型,我会将null作为默认值。这样我知道如果值为null,则调用者不希望为该值指定任何值。

public void Foo(string text, string text2= null);

If the parameter is normally a positive integer, I can use a negative number

如果参数通常是正整数,我可以使用负数

public void Foo(string text, int index=-1);

How about DateTime? It is not nullable, and (as far as I know) it doesn't have a meaningless number that cannot be a true input either (like -1 for positive integer). Or is there? What would you use in this situation?

DateTime怎么样?它不是可空的,并且(据我所知)它没有无意义的数字,也不能成为真正的输入(如正整数的-1)。或者有吗?在这种情况下你会用什么?

I also know that I can use the nullable DateTime type, but this means that the method caller will have to use Nullable as well as opposed to just conveniently pass a DateTime.

我也知道我可以使用可以为空的DateTime类型,但这意味着方法调用者必须使用Nullable以及反对只是方便地传递DateTime。

10 个解决方案

#1


31  

You can make value types nullable using the ? operator in C#:

您可以使用?使值类型为空? C#中的运算符:

DateTime? myDate = null;

约会时间? myDate = null;

From this, you can make the parameter optional:

从这里,您可以使参数可选:

void Foo(DateTime? myDate = null)
{
}

Further reading on Nullable Types.

关于Nullable类型的进一步阅读。

This is not the only way to skin the cat however, you can use default(DateTime), however you cannot use DateTime.MinValue, MaxValue, or Now in optional parameters because they are not compile time constants.

这不是剥皮猫的唯一方法,但是你可以使用默认(DateTime),但是你不能在可选参数中使用DateTime.MinValue,MaxValue或Now,因为它们不是编译时常量。

Of course, you don't need to use optional parameters, you can use overloaded methods if you wish to make use of Min, Max, or Now.

当然,您不需要使用可选参数,如果您希望使用Min,Max或Now,则可以使用重载方法。

void Foo()
{
    Foo(DateTime.MinValue);
}

void Foo(DateTime d)
{
}

If you want to go overkill (well, maybe not overkill, plenty of valid reasons to do this), then you could define a new date type that understands when it has a value:

如果你想过度杀戮(好吧,也许不是矫枉过正,有很多正当理由去做),那么你可以定义一个新的日期类型,它可以理解它何时有一个值:

class SmarterDateTime
{
    public bool IsSet { get; set; }

    // Wrapper around DateTime etc excluded.
}

As for what should be the default, you can choose to make any date represent a default if you wish, but for things like optional parameters you'll have limitations.

至于什么应该是默认值,你可以选择让任何日期代表默认值,但是对于像可选参数这样的东西你会有限制。

Personally, I tend to use DateTime.MinValue.

就个人而言,我倾向于使用DateTime.MinValue。

#2


14  

default (DateTime) - operator default is intended for It

default(DateTime) - 运算符默认值适用于它

#3


6  

At the question "what can be a default value for a DateTime" the response must be: you can only use default(DateTime). This because the default value must be const and both DateTime.MinValue and DateTime.MaxValue are only static readonly, but note that

在“什么可以是DateTime的默认值”的问题上,响应必须是:您只能使用默认值(DateTime)。这是因为默认值必须是const,并且DateTime.MinValue和DateTime.MaxValue都只是静态只读,但请注意

default(DateTime) == DateTime.MinValue

down to the Kind.

到了善良。

If you want you can implement an overload with one less parameter (the DateTime) and from that overload call the "main" method passing the value you prefer.

如果你想要你可以用一个较少的参数(DateTime)实现一个重载,并从那个重载调用“main”方法传递你喜欢的值。

But as written by others, the problem is that you wrote wrong premises.

但正如其他人所写,问题是你写错了前提。

No, DateTime (as nearly all the ValueTypes. Nearly all because Nullable<Nullable<int>> is illegal, even while Nullable<T> is a ValueType) is nullable. Nullable<DateTime> or DateTime? (same thing)

不,DateTime(几乎所有的ValueTypes。几乎所有因为Nullable >都是非法的,即使Nullable 是ValueType)也可以为空。可以为空的 还是DateTime? (一样)

Even int are nullable, you know? int? :-)

即使是int也可以为空,你知道吗?诠释? :-)

#4


5  

DateTime.MinValue will be the default value.

DateTime.MinValue将是默认值。

#5


4  

check the dateTime default parameter , its value would be 1/1/0001 12:00:00 AM,

检查dateTime默认参数,其值为1/1/0001 12:00:00 AM,

private void M(Int32 x = 9, String s = “A”, DateTimedt = default(DateTime), Guidguid = new Guid()) {
Console.WriteLine(“x={0}, s={1}, dt={2}, guid={3}”, x, s, dt, guid);
}

#6


3  

If you use the Nullable the callers of your function can just pass a regular DateTime to it, so they won't notice a thing :) There are implicit operators that will do this for you

如果你使用Nullable你的函数的调用者可以只传递一个常规的DateTime,所以他们不会注意到一件事:)有隐式运算符会为你做这个

If you want to set a default in your function you can do:

如果要在函数中设置默认值,可以执行以下操作:

public void Foo(DateTime? value = null)
{
    if ( value == null )
    {
        value = ... // default
    } 
}

#7


2  

Code Snippet

代码片段

public DateTime method1()
{

  if (condition)

    return new DateTime(2007, 5, 30, 11, 32, 00);

  else

    return default(DateTime);

}

The default statement will initialise a value type to it's default value. In the case of a datetime this value is also exposed as a static property called DateTime.MinValue. If using C# 1.0 the statement "default(DateTime)" would be equivalent to "DateTime.MinValue". You could use this special value as a kind of "marker" value, meaning if it is returned it indicates an invalid datetime.

默认语句将值类型初始化为其默认值。在日期时间的情况下,该值也作为名为DateTime.MinValue的静态属性公开。如果使用C#1.0,​​语句“default(DateTime)”将等同于“DateTime.MinValue”。您可以将此特殊值用作一种“标记”值,这意味着如果返回它,则表示无效的日期时间。

If using C# 2.0 again, it is also possible to use what is called a nullable type, and actually return NULL, as shown in the following example

如果再次使用C#2.0,也可以使用所谓的可空类型,并实际返回NULL,如下例所示

Code Snippet

代码片段

public DateTime? method2()
{

  if (condition)

    return new DateTime(2007, 5, 30, 11, 32, 00);

  else

    return null;

}

#8


1  

    // This is the best way to null out the DateTime.
    //
    DateTime dateTime2 = DateTime.MinValue;

#9


1  

You might consider using the value DateTime.MinValue and use overloading.

您可以考虑使用值DateTime.MinValue并使用重载。

#10


0  

Depends on your use-case.

取决于您的用例。

Any that won't match real data will work, but that depends on your use of it (so in a way does -1 for integers, since it's a perfectly good integer only for your use of it being one were only positive integers make sense).

任何与真实数据不匹配的东西都会起作用,但这取决于你对它的使用(所以在某种程度上整数是-1,因为它只是一个非常好的整数,只有你使用它才是正整数才有意义)。

If you are sending a minimum date (interested in all foo that are later) then any date prior to the earliest sensible date will do, and code like .Where(f -> f.When > myDate) will work without even having to look for that special case.

如果你要发送一个最小日期(对所有foo以后的日期感兴趣),那么在最早的合理日期之前的任何日期都可以,并且代码如.Where(f - > f.When> myDate)将无需查看对于那个特例。

Likewise with maximum dates in reverse (any date that would be after the latest sensible date).

同样,最大日期反转(任何日期将在最新的合理日期之后)。

Otherwise, just avoid the use of defaults entirely, and overload instead.

否则,只需完全避免使用默认值,而是重载。

#1


31  

You can make value types nullable using the ? operator in C#:

您可以使用?使值类型为空? C#中的运算符:

DateTime? myDate = null;

约会时间? myDate = null;

From this, you can make the parameter optional:

从这里,您可以使参数可选:

void Foo(DateTime? myDate = null)
{
}

Further reading on Nullable Types.

关于Nullable类型的进一步阅读。

This is not the only way to skin the cat however, you can use default(DateTime), however you cannot use DateTime.MinValue, MaxValue, or Now in optional parameters because they are not compile time constants.

这不是剥皮猫的唯一方法,但是你可以使用默认(DateTime),但是你不能在可选参数中使用DateTime.MinValue,MaxValue或Now,因为它们不是编译时常量。

Of course, you don't need to use optional parameters, you can use overloaded methods if you wish to make use of Min, Max, or Now.

当然,您不需要使用可选参数,如果您希望使用Min,Max或Now,则可以使用重载方法。

void Foo()
{
    Foo(DateTime.MinValue);
}

void Foo(DateTime d)
{
}

If you want to go overkill (well, maybe not overkill, plenty of valid reasons to do this), then you could define a new date type that understands when it has a value:

如果你想过度杀戮(好吧,也许不是矫枉过正,有很多正当理由去做),那么你可以定义一个新的日期类型,它可以理解它何时有一个值:

class SmarterDateTime
{
    public bool IsSet { get; set; }

    // Wrapper around DateTime etc excluded.
}

As for what should be the default, you can choose to make any date represent a default if you wish, but for things like optional parameters you'll have limitations.

至于什么应该是默认值,你可以选择让任何日期代表默认值,但是对于像可选参数这样的东西你会有限制。

Personally, I tend to use DateTime.MinValue.

就个人而言,我倾向于使用DateTime.MinValue。

#2


14  

default (DateTime) - operator default is intended for It

default(DateTime) - 运算符默认值适用于它

#3


6  

At the question "what can be a default value for a DateTime" the response must be: you can only use default(DateTime). This because the default value must be const and both DateTime.MinValue and DateTime.MaxValue are only static readonly, but note that

在“什么可以是DateTime的默认值”的问题上,响应必须是:您只能使用默认值(DateTime)。这是因为默认值必须是const,并且DateTime.MinValue和DateTime.MaxValue都只是静态只读,但请注意

default(DateTime) == DateTime.MinValue

down to the Kind.

到了善良。

If you want you can implement an overload with one less parameter (the DateTime) and from that overload call the "main" method passing the value you prefer.

如果你想要你可以用一个较少的参数(DateTime)实现一个重载,并从那个重载调用“main”方法传递你喜欢的值。

But as written by others, the problem is that you wrote wrong premises.

但正如其他人所写,问题是你写错了前提。

No, DateTime (as nearly all the ValueTypes. Nearly all because Nullable<Nullable<int>> is illegal, even while Nullable<T> is a ValueType) is nullable. Nullable<DateTime> or DateTime? (same thing)

不,DateTime(几乎所有的ValueTypes。几乎所有因为Nullable >都是非法的,即使Nullable 是ValueType)也可以为空。可以为空的 还是DateTime? (一样)

Even int are nullable, you know? int? :-)

即使是int也可以为空,你知道吗?诠释? :-)

#4


5  

DateTime.MinValue will be the default value.

DateTime.MinValue将是默认值。

#5


4  

check the dateTime default parameter , its value would be 1/1/0001 12:00:00 AM,

检查dateTime默认参数,其值为1/1/0001 12:00:00 AM,

private void M(Int32 x = 9, String s = “A”, DateTimedt = default(DateTime), Guidguid = new Guid()) {
Console.WriteLine(“x={0}, s={1}, dt={2}, guid={3}”, x, s, dt, guid);
}

#6


3  

If you use the Nullable the callers of your function can just pass a regular DateTime to it, so they won't notice a thing :) There are implicit operators that will do this for you

如果你使用Nullable你的函数的调用者可以只传递一个常规的DateTime,所以他们不会注意到一件事:)有隐式运算符会为你做这个

If you want to set a default in your function you can do:

如果要在函数中设置默认值,可以执行以下操作:

public void Foo(DateTime? value = null)
{
    if ( value == null )
    {
        value = ... // default
    } 
}

#7


2  

Code Snippet

代码片段

public DateTime method1()
{

  if (condition)

    return new DateTime(2007, 5, 30, 11, 32, 00);

  else

    return default(DateTime);

}

The default statement will initialise a value type to it's default value. In the case of a datetime this value is also exposed as a static property called DateTime.MinValue. If using C# 1.0 the statement "default(DateTime)" would be equivalent to "DateTime.MinValue". You could use this special value as a kind of "marker" value, meaning if it is returned it indicates an invalid datetime.

默认语句将值类型初始化为其默认值。在日期时间的情况下,该值也作为名为DateTime.MinValue的静态属性公开。如果使用C#1.0,​​语句“default(DateTime)”将等同于“DateTime.MinValue”。您可以将此特殊值用作一种“标记”值,这意味着如果返回它,则表示无效的日期时间。

If using C# 2.0 again, it is also possible to use what is called a nullable type, and actually return NULL, as shown in the following example

如果再次使用C#2.0,也可以使用所谓的可空类型,并实际返回NULL,如下例所示

Code Snippet

代码片段

public DateTime? method2()
{

  if (condition)

    return new DateTime(2007, 5, 30, 11, 32, 00);

  else

    return null;

}

#8


1  

    // This is the best way to null out the DateTime.
    //
    DateTime dateTime2 = DateTime.MinValue;

#9


1  

You might consider using the value DateTime.MinValue and use overloading.

您可以考虑使用值DateTime.MinValue并使用重载。

#10


0  

Depends on your use-case.

取决于您的用例。

Any that won't match real data will work, but that depends on your use of it (so in a way does -1 for integers, since it's a perfectly good integer only for your use of it being one were only positive integers make sense).

任何与真实数据不匹配的东西都会起作用,但这取决于你对它的使用(所以在某种程度上整数是-1,因为它只是一个非常好的整数,只有你使用它才是正整数才有意义)。

If you are sending a minimum date (interested in all foo that are later) then any date prior to the earliest sensible date will do, and code like .Where(f -> f.When > myDate) will work without even having to look for that special case.

如果你要发送一个最小日期(对所有foo以后的日期感兴趣),那么在最早的合理日期之前的任何日期都可以,并且代码如.Where(f - > f.When> myDate)将无需查看对于那个特例。

Likewise with maximum dates in reverse (any date that would be after the latest sensible date).

同样,最大日期反转(任何日期将在最新的合理日期之后)。

Otherwise, just avoid the use of defaults entirely, and overload instead.

否则,只需完全避免使用默认值,而是重载。