Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I default a parameter to DateTime.MaxValue in C#?

I wish to say:

public void Problem(DateTime optional = DateTime.MaxValue)
{
}

But the compiler complains that DateTime.MaxValue is not a compile time constant.

DateTime.MinValue is easy, just use default(DateTime)

see also "How do I default a parameter to Guid.Empty in C#?"

I do not wish to use method overloading, as the method I am trying to tame has 101 parameters!

like image 499
Ian Ringrose Avatar asked Apr 04 '11 13:04

Ian Ringrose


1 Answers

I would substitute this for something like:

public void Problem(DateTime? optional = null)
{
   DateTime dateTime = optional ?? DateTime.MaxValue
   // Now use dateTime
}
like image 127
Ilya Kogan Avatar answered Sep 27 '22 16:09

Ilya Kogan