Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Why are DateTime.MinValue and MaxValue not compile-time constants?

I wanted to have an optional date parameter for a method (defaulted to MinValue), in order to check if the user had actually supplied a value or not (supplying MinValue was invalid), but I'm not allowed as apparently it's not a compile-time constant.

According to the MSDN page, "The value of this constant is equivalent to 00:00:00.0000000, January 1, 0001."

So why is that not compile-time constant? And why is it different from passing in Int32.MinValue, which is allowed?

like image 940
Alex Avatar asked Jul 28 '11 09:07

Alex


1 Answers

You cannot define a DateTime constant (or structs). From MSDN allowed types for const are:

One of the types: byte, char, short, int, long, float, double, decimal, bool, string, an enum type, or a reference type.

like image 179
Vasea Avatar answered Sep 28 '22 07:09

Vasea