What should be the default value in a DateTime optional parameter?

59,025

Solution 1

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

DateTime? myDate = null;

From this, you can make the parameter optional:

void Foo(DateTime? myDate = null)
{
}

Further reading on Nullable Types.

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.

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.

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.

Solution 2

default (DateTime) - operator default is intended for It

Solution 3

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

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.

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)

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

Solution 4

DateTime.MinValue will be the default value.

Solution 5

check the dateTime default parameter , its value would be 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);
}
Share:
59,025
Louis Rhys
Author by

Louis Rhys

trying to learn and help others learn :)

Updated on January 09, 2020

Comments

  • Louis Rhys
    Louis Rhys over 4 years

    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.

    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?

    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.

  • Louis Rhys
    Louis Rhys over 12 years
    I know, but this is a bit undesirable, because the caller code will have to call it with a nullable as well
  • Louis Rhys
    Louis Rhys over 12 years
    what is the actual value of it?
  • Jon Skeet
    Jon Skeet over 12 years
    @LouisRhys: Not explicitly they won't - there's an implicit conversion from DateTime to DateTime?.
  • Jon Skeet
    Jon Skeet over 12 years
    That doesn't "null out" a DateTime. It's still a valid value. How would you distinguish between deliberately specifying that value, and one which is "absent"?
  • Louis Rhys
    Louis Rhys over 12 years
    @JonSkeet @adam is there? But still, I remember that sometimes I encounter situations where I expect the code would work with a DateTime but doesn't work with a DateTime? (not sure what the code was, though)
  • Jon Skeet
    Jon Skeet over 12 years
    @LouisRhys: That would be the case with a ref parameter, or if you're fetching a DateTime? value and trying to assign it to a DateTime variable.
  • Louis Rhys
    Louis Rhys over 12 years
    @JonSkeet Does that mean that if it's just a non-ref parameter the caller won't be affected at all?
  • Jon Skeet
    Jon Skeet over 12 years
    @LouisRhys: Well, it won't be a binary-compatible change, and there are some other corner cases (method group conversions for delegates) but for a "normal" method call it should be fine.
  • Louis Rhys
    Louis Rhys over 12 years
    by "overload instead" do you mean repeat the method body in both methods?
  • Jon Hanna
    Jon Hanna over 12 years
    Ideally as much possible would be shared either by one calling the other, or by both calling a private method with the bulk of the code. Just where the balance lies there depends on what you are doing in said methods.
  • Louis Rhys
    Louis Rhys over 12 years
    Yes, I agree, but if one calls the other or both call the same method, we still have to solve the original problem, don't we?
  • Jon Hanna
    Jon Hanna over 12 years
    Only if that's the case, though even then it gets around the fact that the default in a signature has to be compile-time constant. There are also cases where one could call the other, and we don't have to solve it, such as IQueryable<Doc> GetDocs(int catID, DateTime published){return GetDocuments(catID).Where(d => d.Published == published);} which doesn't need to worry about it at all. There's also DateTime? which you dismiss above, but neglect that a method with a DateTime? parameter can be called with a DateTime argument, hence being just as convenient.
  • natli
    natli over 11 years
    For anyone else wondering, the actual value is: 1-1-0001 0:00:00
  • Kjartan
    Kjartan about 11 years
    It is equivalent to DateTime.MinValue (Midnight on the first day of the first month in the first year).
  • da_jokker
    da_jokker over 7 years
    Using MVC, I had a DateTime as optional, but the function I was calling to try and convert to a datetime wouldn't work. Adding the ? to the Function return type totally fixed it.. THANKS!
  • spacebread
    spacebread almost 7 years
    default(DateTime?) is null