Set default value for DateTime in optional parameter

C#DatetimeDefaultOptional Parameters

C# Problem Overview


How can I set default value for DateTime in optional parameter?

public SomeClassInit(Guid docId, DateTime addedOn = DateTime.Now???)
{
    //Init codes here
}

C# Solutions


Solution 1 - C#

There is a workaround for this, taking advantage of nullable types and the fact that null is a compile-time constant. (It's a bit of a hack though, and I'd suggest avoiding it unless you really can't.)

public void SomeClassInit(Guid docId, DateTime? addedOn = null)
{
    if (!addedOn.HasValue)
        addedOn = DateTime.Now;

    //Init codes here
}

In general, I'd prefer the standard overloading approach suggested in the other answers:

public SomeClassInit(Guid docId)
{
    SomeClassInit(docId, DateTime.Now);
}

public SomeClassInit(Guid docId, DateTime addedOn)
{
    //Init codes here
}

Solution 2 - C#

I guess that you did not really want addedOn = DateTime.Now because that would suggest you never get any result as everything would be added before 'Now'. :)

A default DateTime can be set like this:

public void SomeClassInit(Guid docId, DateTime addedOn = default(DateTime))

Update
If you deal with SQL Server, do not forget that it doesn't accept default(DateTime) what is 1/1/0001. SQL Server's minimal DateTime is 1/1/1753 (explanation). SQL's DateTime2 accepts 1/1/0001, though.

Solution 3 - C#

I'd slightly modify LukeH's solution as:

public void SomeClassInit(Guid docId, DateTime? addedOn = null)
{
    DateTime TargetDateTimeProperty = addedOn ?? DateTime.Now;
}

which is shorter and more readable, it seems.

Solution 4 - C#

Don't use an optional parameter:

public SomeClassInit(Guid docId, DateTime addedOn)
{
    SomeClassInitCore(docId, addedOn);
}

public SomeClassInit(Guid docId)
{
    SomeClassInitCore(docId, null);
}

private SomeClassInitCore(Guid docId, DateTime? addedOn)
{
    // set default value
    if (addedOn.IsNull) addedOn = DateTime.Now;

    //Init codes here
}

Solution 5 - C#

.NET 4.0 does have optional parameters. (google is also your friend, here.)

EDIT (because of Anthony Pegram correct, comment)...

And yes, that is how you would do it.

But DateTime.Now (static property, on that class) is not know until run-time. As such, you can't use that as an optional value.

.NET 3.5 doesn't ... so then you would have to do what JS Bangs said...

public SomeClassInit(Guid docId) 
{ 
    return SomeClassInit(docId, DateTime.Now);
}

public SomeClassInit(Guid docId, DateTime addedOn = DateTime.Now???) 
{ 
    //Init codes here 
}

or even the null checking/null value parameter from munificent's answer.

Cheers Anthony.

Solution 6 - C#

C# doesn't have optional parameters in this sense. If you want to make addedOn optional, you should write an overload that doesn't require that parameter, and passes DateTime.Now to the two-argument version.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSadeghView Question on Stackoverflow
Solution 1 - C#LukeHView Answer on Stackoverflow
Solution 2 - C#Rick GlimmerView Answer on Stackoverflow
Solution 3 - C#Alexey KhoroshikhView Answer on Stackoverflow
Solution 4 - C#munificentView Answer on Stackoverflow
Solution 5 - C#Pure.KromeView Answer on Stackoverflow
Solution 6 - C#JSBձոգչView Answer on Stackoverflow