How can I create an optional DateTime parameter?

后端 未结 4 1068
悲哀的现实
悲哀的现实 2021-02-18 23:30

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. The function i

4条回答
  •  爱一瞬间的悲伤
    2021-02-18 23:55

    Btw, you don't have to use nullable datetime like all other answers says. You can do it with DateTime as well:

    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.

提交回复
热议问题