Set default value for DateTime in optional parameter [duplicate]
Asked Answered
B

6

114

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

public SomeClassInit(Guid docId, DateTime addedOn = DateTime.Now???)
{
    //Init codes here
}
Bast answered 13/6, 2010 at 4:47 Comment(1)
This is not a duplicate Timespans work differently in c# than timespans. This is doable in c# with DateTimeConstantAttribute but timespans do not support this.Unconscious
S
172

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
}
Stencil answered 13/6, 2010 at 6:28 Comment(4)
I don't really understand why optional params must be compile-time constants. The compiler could easily create the 2 methods you wrote from the one with the optional param...I usually prefer having one method with several optional params instead of several methods, it makes the code smaller and thus easier to read.Uncial
You can use coalesce here: addedOn = addedOn ?? DateTime.NowDebbiedebbra
Nullables can cause casting problems deep in your code, so I am not a fan. I am becoming increasing disappointed in C#. I mean, even VB.net has literal dates, such as: Optional addedOn As DateTime = #12:00:00 PM#Larson
VS wouldnt compile with this code. Instead i used DateTime date = default(DateTime), and then checked to see if date==default(DateTime)Merrick
C
60

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.

Commeasure answered 20/6, 2012 at 2:34 Comment(4)
Dont understand why this isnt marked as the answer, it allows for much easier working with datetime, since datetime? != datetimeCopperas
This is the correct answer.Brittaniebrittany
I'm just going to voice what others have said, this IS the correct answer. The others are basically just cheating the system.Resurge
I suppose this is gong to return a default value for datetime. what if someone wants the current datetime ?Oma
B
30

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.

Brimstone answered 25/7, 2011 at 8:15 Comment(1)
It is not more readable but it allows me to access the .Year/.Month filelds.Chirpy
D
8

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
}
Dense answered 13/6, 2010 at 4:52 Comment(0)
O
6

.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.

Othilie answered 13/6, 2010 at 4:53 Comment(3)
The default value must be a compile time constant, so this is not going to work even in 4.0.Interrelated
Confirmed Anothony. I'll re-edit my post. Cheers.Othilie
This Shoud Be The Accepted Answer (TSBTAA). This is the only answer that actually and clearly explains why it's not possible to use common DateTime "values" for optional argument.Ragland
T
2

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.

Tanka answered 13/6, 2010 at 4:50 Comment(3)
er.. sure abot that JS Bangs? for framework version .NET 3.5 and below, you are correct. otherwise, 4.0 and above, it is supported.Othilie
@Pure.Krome, default values for optional parameters must be compile time constants. So this is not supported. (This answer could be worded better, however.)Interrelated
i too misunderstood the wording. soz JS.Othilie

© 2022 - 2024 — McMap. All rights reserved.