Default parameter for value must be a compile time constant?
Use regular method overloads instead:
public static void DatesToPeriodConverter(DateTime start, out string date, out string time)
{
DatesToPeriodConverter(start, DateTime.MinValue, out date, out time);
}
public static void DatesToPeriodConverter(DateTime start, DateTime end, out string date, out string time)
{ }
Atlernatively, default(DateTime)
is the same as DateTime.MinValue
and is compile time constant, but I tend to err away from using this style (there's no guarantee in future that default(DateTime)
will equal DateTime.MinValue
):
public static void DatesToPeriodConverter(DateTime start, DateTime end = default(DateTime), out string date, out string time)
Or as Marc suggests, use DateTime?
which allows a null
default value.
DateTime.MinValue
is not a const
, because the language doesn't like const
on DateTime
. One option is to use DateTime?
instead, i.e.
public static void DatesToPeriodConverter(DateTime start, DateTime? end = null,
out string date, out string time)
{
var effectiveEnd = end ?? DateTime.MinValue;
// ...
}
However, you will still have the issue of having non-default parameters after default parameters - you may need to re-order them to use that as a default.
You can try doing it this way:
public static void DatesToPeriodConverter(DateTime start, DateTime? end , out string date, out string time)
{
if(!end.HasValue){
end = DateTime.MinValue;
}
}
Change a type of the parameter end to a Nullable and use null as a default value:
public static void DatesToPeriodConverter(DateTime start, DateTime? end = null, out string date, out string time)
or use default(DateTime) as a default value:
public static void DatesToPeriodConverter(DateTime start, DateTime end = default(DateTime), out string date, out string time)