简体   繁体   English

如何创建可选的DateTime参数?

[英]How can I create an optional DateTime parameter?

I have this function that returns a reference type. 我有这个函数返回一个引用类型。 Now, this function has two optional parameters both of which are instances of the DateTime class. 现在,这个函数有两个可选参数,它们都是DateTime类的实例。 The function is something like this: 功能是这样的:

public DateTime GetDate(DateTime start = DateTime.MinValue, DateTime end = DateTime.MinValue)
{
    // Method body...
}

The error from VS is: VS的错误是:

Default parameter value for 'start' must be a compile-time constant 'start'的默认参数值必须是编译时常量

Of course, the error applies to the second parameter and I perfectly understand what is happening. 当然,错误适用于第二个参数,我完全理解发生了什么。

What I really want is to know if there is a way to go about this, that is, having optional parameters in the method. 我真正想要的是知道是否有办法解决这个问题,即在方法中有可选参数。 Right now, what I have done is to create an overload; 现在,我所做的就是创造一个过载; I mean, I have created a parameterless function GetDate() and a two-parameter overload of it. 我的意思是,我创建了一个无参数函数GetDate()和它的双参数重载。

This is not really a problem but I just want to know if there is a way to do it. 这不是一个真正的问题,但我只是想知道是否有办法做到这一点。

One workaround is to assign them like this: 一种解决方法是像这样分配它们:

public DateTime GetDate(DateTime? start = null, DateTime? end = null){
    start = start ?? DateTime.MinValue;
    end = end ?? DateTime.MinValue;

    Console.WriteLine ("start: " + start);
    Console.WriteLine ("end: " + end);
    return DateTime.UtcNow;
}

Which can be used like this: 哪个可以这样使用:

void Main()
{
    new Test().GetDate();
    new Test().GetDate(start: DateTime.UtcNow);
    new Test().GetDate(end: DateTime.UtcNow);
    new Test().GetDate(DateTime.UtcNow, DateTime.UtcNow);
}

And works just as expected: 并按预期工作:

start: 1/01/0001 0:00:00
end: 1/01/0001 0:00:00

start: 8/08/2014 17:30:29
end: 1/01/0001 0:00:00

start: 1/01/0001 0:00:00
end: 8/08/2014 17:30:29

start: 8/08/2014 17:30:29
end: 8/08/2014 17:30:29

Note the named parameter to distinguish between the start and end value. 请注意命名参数以区分start值和end值。

Btw, you don't have to use nullable datetime like all other answers says. 顺便说一下,你不必像所有其他答案那样使用可空的日期时间。 You can do it with DateTime as well: 您也可以使用DateTime来完成:

public DateTime GetDate(
     DateTime start = default(DateTime), 
     DateTime end = default(DateTime))
{
     start = start == default(DateTime) ? DateTime.MinValue : start;
     end = end == default(DateTime) ? DateTime.MinValue : end;
}

This is unlikely but it won't work as expected if you actually pass the default datetime value to your function. 这不太可能,但如果您实际将默认日期时间值传递给函数,它将无法按预期工作。

You can set the DateTime to be nullable and then convert to DateTime.Min if no parameter is provided: 您可以将DateTime设置为可为空,然后如果未提供参数则转换为DateTime.Min

public DateTime GetDate(DateTime? start = null, DateTime? end = null) {
    var actualStart = start ?? DateTime.Min;
    var actualEnd = end ?? DateTime.Min;
}

The only way would be to do it like this (a bit more code, but it gives you optional args): 唯一的方法是这样做(更多的代码,但它给你可选的args):

public DateTime GetDate(DateTime? start = null, DateTime? end = null)
{
    // Method body...
    if(start == null)
    {
      start = DateTime.MinValue;
    }

    //same for end
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM