ChatGPT解决这个技术问题 Extra ChatGPT

new DateTime() vs default(DateTime)

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.

Which is equal to DateTime.MinValue, so you could also just do DateTime myDate = DateTime.MinValue as well :/
@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.
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.

S
Servy

No, they are identical.

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


FWIW; In C# 6 this behavior will change. C# 6 introduces parameterless constructors for structs, which allow the behavior of new to differ from what default(T) will do.
One could argue that default(DateTime) is more descriptive of the programmer's intent, therefore more favorable usually.
@vcsjones this was removed before final release of C# 6.0 however.
@nawfal Yeah. Probably best that it did, too.
@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.
T
Tomer

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

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)
j
jpaugh

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.


how does that answer the OP's question?
Please see the first 4 words of my answer.
I'd argue against this. The default DateTime is useful as a a value-not-set value, with a simple comparison to default(DateTime). What use is a non-default 01/01/0001 value?
Bear in mind that it's DateTimeKind.Utc instead of DateTimeKind.UTC
@Roberto Thanks, fixed.
G
G.Busato

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


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.