88

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) 
1
  • @Marc Gravell: my bad.. I thought I looked everywhere.. I'm going to delete my comment... Just to strengthen the idea so that any visitor can notice it easily: So it's not just the issue with the constant value, there are actually 2 totally different reasons why one would want to write overloads.. The second one is the order of out and optional parameters. Both these types of parameters are required to be last. Commented Sep 11, 2013 at 12:03

8 Answers 8

123

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I think this is the best solution. I usually prefer to go with null rather than .MinValue/.MaxValue and other "magic" values as defaults. It's clearer, less error-prone and more precise: null means no value was given. No more, no less.
@student I use several here; the DateTime? means Nullable<DateTime>, meaning "a value that can either be a DateTime or null"; the ?? is the null-coalescing operator, which means (in this context): take the first non-null value, i.e. take end if it is non-null, otherwise DateTime.MinValue
I much prefer this approach compared to method overloading
@Ando because it can at least be expressed in the language, with the value in the body code
end ??= DateTime.MinValue;
|
31

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.

7 Comments

Aside: it is intriguing that =default(DateTime) works here, when const DateTime x = default(DateTime); does not.
@MarcGravell Crazy, you are right, I've never noticed that. Might make a good question.
@MarcGravell Found out the answer, seems to be a compiler red-herring, as const int MyInt = default(int) works fine, the compiler first complains that DateTime is not actually a valid const.
@MarcGravell: It makes perfect sense when you consider that =null works for references, even though you can't declare a reference as const. Supporting const for datatypes which have exactly one valid const strikes me as somewhat unnecessary. On the other hand, supporting =null for default arguments is obviously useful.
I wanted to do something like class Foo { public Foo(List<string> items = new List<string>()) { } }. Changing new List<string>() to default(List<string>) solved the problem for me
|
7

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) 

Comments

7

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

2 Comments

Work great! Can simplify this using Coalesce, replace the entire if statement with: end = (end ?? DateTime.MinValue)
I like this methods because it stops the propagation of nullable types through your code.
2

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.

1 Comment

Yes you can use DateTime? if it is ok with you according to your code logic.
1

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

Comments

1

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>(); 

Comments

-3

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,

1 Comment

There's no way to assign a value to your constant datetime, as there are no compile time literals for DateTime.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.