249

Is there a reason to choose one of these over the other?

DateTime myDate = new DateTime();

or

DateTime myDate = default(DateTime);

Both of them are equal 1/1/0001 12:00:00 AM.

shA.t
  • 16,580
  • 5
  • 54
  • 111
RJP
  • 4,016
  • 5
  • 29
  • 42
  • 34
    Which is equal to `DateTime.MinValue`, so you could also just do `DateTime myDate = DateTime.MinValue` as well :/ – Lloyd Dec 19 '12 at 17:22
  • 28
    @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 Aug 29 '14 at 14:09
  • 21
    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 Dec 30 '14 at 16:20

4 Answers4

251

No, they are identical.

default(), for any value type (DateTime is a value type) will always call the parameterless constructor.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • 33
    FWIW; In C# 6 this behavior will change. C# 6 introduces [parameterless constructors](https://roslyn.codeplex.com/discussions/562559) for structs, which allow the behavior of `new` to differ from what `default(T)` will do. – vcsjones Feb 27 '15 at 15:45
  • 35
    One could argue that `default(DateTime)` is more descriptive of the programmer's intent, therefore more favorable usually. – intrepidis Jul 08 '15 at 09:34
  • 16
    @vcsjones this was removed before final release of C# 6.0 however. – nawfal Jul 05 '16 at 13:08
  • 5
    @nawfal Yeah. Probably best that it did, too. – vcsjones Jul 05 '16 at 13:59
  • This answer is just wrong IMO: the two things are semantically identical, but neither of them calls a constructor - both syntax options use `initobj` – Marc Gravell Jun 18 '20 at 06:22
  • @MarcGravell The underlying implementation is irrelevant. From the perspective of the language, this is the semantic meaning of it. That a particular implementation might accomplish that using a different means isn't really relevant. – Servy Jun 18 '20 at 13:48
  • @Servy I agree to a point; I simply disagree on the phrasing; I would say that `new DateTime()` doesn't mean "call the parameterless constructor", because there *is* no parameterless constructor *to call*; I would argue instead that `new DateTime()` means "initializes a default `DateTime`", as does `default(DateTime)`, as does `default` when used in the context of `DateTime`. You can't call something that doesn't exist. – Marc Gravell Jun 18 '20 at 14:16
  • 1
    @MarcGravell From the perspective of the language there *is* a parameterless constructor. That one implementation of the language's runtime optimizes it out doesn't change that. `new DateTime()` *does* mean "call the parameterless constructor", even if the MS CLR implements it radically differently than it does for a constructor with parameters, and if the runtime happens to not actually have a parameterless constructor for that type for that reason. – Servy Jun 18 '20 at 17:21
  • @MarcGravell The runtime is different than the language. A huge portion of the statements about C# as a language don't apply to the runtime, that doesn't mean that they're wrong, it just means the runtime is a different thing than the language. If this was a question about the MS CLR, not the C# language, then you are right that my statements would be wrong. – Servy Jun 18 '20 at 17:21
  • There _are_ no constructors at all! There are only ones and zeros! And even that is up for debate! – Bitterblue Jul 14 '20 at 14:00
25

If you want to use default value for a DateTime parameter in a method, you can only use default(DateTime).

The following line will not compile:

    private void MyMethod(DateTime syncedTime = DateTime.MinValue)

This line will compile:

    private void MyMethod(DateTime syncedTime = default(DateTime))
Tomer
  • 294
  • 3
  • 4
  • 3
    Of course DateTime.MinValue doesn't compile? Optional parameters must be compile-time constants, which MinValue is not. default(DateTime) and new DateTime() are both valid though.(which is actually funny, because const DateTime x = default(DateTime) is not valid, since DateTime values cannot be declared as compile-time constants) – Jerri Kangasniemi Oct 28 '16 at 08:56
18

The answer is no. Keep in mind that in both cases, mdDate.Kind = DateTimeKind.Unspecified.

Therefore it may be better to do the following:

DateTime myDate = new DateTime(1, 1, 1, 0, 0, 0, DateTimeKind.Utc);

The myDate.Kind property is readonly, so it cannot be changed after the constructor is called.

jpaugh
  • 6,634
  • 4
  • 38
  • 90
Ben C
  • 199
  • 1
  • 2
-4

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

gBusato
  • 811
  • 7
  • 8
  • 5
    No, structs are *not* necessarily initialized to their minimum value. They're initialized to all bits being zero. For some types, that's their lowest value, for others, such as numeric types that can be negative, it's no where near their minimum value. And of course, others still won't be comparable and won't *have* a "minimum" value. – Servy Feb 27 '19 at 15:53