Ankit
Ankit

Reputation: 6622

Default parameter for value must be a compile time constant?

This is my method signature. While trying to pass end as an optional parameter it gives me this error. What should I do to resolve this? Why isn't DateTime.MinValue a constant?

public static void DatesToPeriodConverter(DateTime start, DateTime end = DateTime.MinValue,
                                          out string date, out string time)

Upvotes: 88

Views: 123744

Answers (8)

Ogglas
Ogglas

Reputation: 69968

This error can happen with Lists as well:

CS1736 Default parameter value for 'dateTimes' must be a compile-time constant

public YourEntity(dateTimes = new List<DateTime>())
{
    DateTimes = dateTimes;
}

public List<DateTime> DateTimes { get; set; } = new List<DateTime>();

You can solve it like this then:

public YourEntity(dateTimes = null)
{
    DateTimes = dateTimes != null ? dateTimes : new List<DateTime>();
}

public List<DateTime> DateTimes { get; set; } = new List<DateTime>();

Upvotes: 1

HydTechie
HydTechie

Reputation: 807

we can create CONSTANTS class with default values

public const int DEFAULTINT = -9999;

and use them as CONSTANTS.DEFAULTINT as business defaults..

hope it helps,

Upvotes: -3

Adam Moszczyński
Adam Moszczyński

Reputation: 3556

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;
    }
}

Upvotes: 7

Eduard Dumitru
Eduard Dumitru

Reputation: 3262

Optional parameters must appear at the end of the parameter list. out parameters must also appear at the end of the parameter list. Your optional parameter is not an out parameter.

Furthermore, you can't use default values for optional parameters other than literal constants and a few weird corner cases.

All facts point in the following direction:

  • Create a secondary overload method.
  • Make the initial method not include the parameter
  • Make the secondary one include the parameter
  • Call your more general method (the one with the parameter) from your more specific one and implement the logic only in the more general one

Upvotes: 1

Viacheslav Ivanov
Viacheslav Ivanov

Reputation: 1555

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)

Upvotes: 7

Adarsh Kumar
Adarsh Kumar

Reputation: 1150

You are correct. Default parameter for value must be a compile time constant. Dynamically calculated value is not accepted by compiler against optional parameter. The reason behind this may be that it is not definite that the dynamic value you are providing would give some valid value.

Upvotes: 2

Marc Gravell
Marc Gravell

Reputation: 1062820

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.

Upvotes: 120

Adam Houldsworth
Adam Houldsworth

Reputation: 64487

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.

Upvotes: 31

Related Questions