Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) 
like image 379
Ankit Avatar asked Sep 11 '13 11:09

Ankit


People also ask

What are the parameter that are default values?

The default parameter is a way to set default values for function parameters a value is no passed in (ie. it is undefined ). In a function, Ii a parameter is not provided, then its value becomes undefined . In this case, the default value that we specify is applied by the compiler.

Can out parameter have a default value?

The designers considered the feature and rejected it as not useful enough to be worth the cost of implementing. The designers considered the feature and rejected it as being confusing because it uses similar syntax to default value parameters, but has a quite different meaning.

What is the default value of TimeSpan?

TimeSpan Range The default value is TimeSpan(0, 0, 0, 0, 0) . MaximumTime(TimeSpan): Defines the upper limit of the available selection range. The default value is TimeSpan(30, 23, 59, 59) .

What is a compile time constant C#?

A variable in C# can be made into a compile-time constant by adding the const keyword before the data type. This modifier means that the variable cannot be changed and it must therefore be assigned a value at the same time as it is declared.


2 Answers

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.

like image 163
Marc Gravell Avatar answered Sep 24 '22 05:09

Marc Gravell


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.

like image 37
Adam Houldsworth Avatar answered Sep 25 '22 05:09

Adam Houldsworth