I was writing a method which takes DateTime
value as one of it's parameters. I decided that it's optional parameter so I went ahead and tried to make DateTime.MinValue
as default.
private void test(string something, DateTime testVar = DateTime.MinValue) {
}
However this gives an error that:
Default parameter value for 'testVar' must be a compile-time constant.
Using this code seems to work just fine.
private void test(string something, DateTime testVar = new DateTime()) {
}
I was given advice to use DateTime.MinValue instead of new DateTime() as it's self-documenting. Since new DateTime()
is basically the same thing why DateTime.MinValue
can't be used? Also will there be any potential problem if I leave it with new DateTime()
?
MinValue defines the date and time that is assigned to an uninitialized DateTime variable. The following example illustrates this. The MinValue and MaxValue properties can be used to ensure that a value lies within the supported range before passing it to a DateTime constructor.
Every optional parameter in the procedure definition must specify a default value. The default value for an optional parameter must be a constant expression. Every parameter following an optional parameter in the procedure definition must also be optional.
By default, all parameters of a method are required. A method that contains optional parameters does not force to pass arguments at calling time. It means we call method without passing the arguments. The optional parameter contains a default value in function definition.
Optional parameters are defined at the end of the parameter list, after any required parameters. If the caller provides an argument for any one of a succession of optional parameters, it must provide arguments for all preceding optional parameters.
DateTime.MinValue
is defined as:
public static readonly DateTime MinValue
Which is not the same as const
. Since a readonly
value is not a compile-time constant (i.e. the value is not evaluated at compile-time), it can't be used.
The reason that using new DateTime()
works is because that expression is known at compile-time. It's the same as writing default(DateTime)
. For example, result == true
in the following expression:
var result = new DateTime() == default(DateTime);
Other answers touch upon why DateTime.MinValue cannot be used, it is not a legal compile time constant. It is a static readonly
field, which might very well be constant as far as usage goes, but is not legally constant, nor does it fit the rules for what can be used as a default argument. As for why new DateTime()
can be used, see section 10.6.1 of the C# 4.0 Language Specification. Relevant bits:
The expression in a default-argument must be one of the following:
· a constant-expression
· an expression of the form new S() where S is a value type
· an expression of the form default(S) where S is a value type
These result in a zero-initialized instance, basically a bit pattern of all zeros. (See: Section 4.1.2)
However, in this case, I still recommend using a DateTime? value = null
as the parameter and default argument, particularly when it's representing a nullable date in a database. MinValue
is not the absence of a value. null
is.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With