Which is equal to DateTime.MinValue, so you could also just do DateTime myDate = DateTime.MinValue as well :/
- Lloyd
@Lloyd Most of the time... but not as a default parameter DateTime.MinValue is not a compile time constant - but default(DateTime)/new DateTime() is.
- Ricibob
Just to clarify @Ricibob's excellent comment, because it's important: if you are creating a method with an optional parameter, you can ONLY use either default(DateTime) or new DateTime(). Those are both compile time constants, required for optional parameter values. If compile time constants are not required, then default(DateTime), new DateTime(), and DateTime.MinValue are interchangeable.
- leanne
4 Answers
225
votes
No, they are identical.
default(), for any value type (DateTime is a value type) will always call the parameterless constructor.
24
votes
If you want to use default value for a DateTime parameter in a method, you can only use default(DateTime).
The myDate.Kind property is readonly, so it cannot be changed after the constructor is called.
-4
votes
The simpliest way to understand it is that DateTime is a struct. When you initialize a struct it's initialize to it's minimum value : DateTime.Min
Therefore there is no difference between default(DateTime) and new DateTime() and DateTime.Min
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.OkRead more
DateTime.MinValue, so you could also just doDateTime myDate = DateTime.MinValueas well :/ - Lloyddefault(DateTime)ornew DateTime(). Those are both compile time constants, required for optional parameter values. If compile time constants are not required, thendefault(DateTime),new DateTime(), andDateTime.MinValueare interchangeable. - leanne