In the below thisIsAlwaysTrue
should always be true.
DateTime d = DateTime.Now;
bool thisIsAlwaysTrue = d == d;
But does DateTime.Now work in such a way that isThisAlwaysTrue is guaranteed to be true? Or can the clock change between references to the Now property?
bool isThisAlwaysTrue = DateTime.Now == DateTime.Now;
The clock can definitely change between two back-to-back calls to DateTime.Now;
The DateTime.Now property is volatile, meaning it definitely can change between uses. But the variable you assign it to is not volatile.
So this should always set result to true:
DateTime d = DateTime.Now;
bool result = d == d;
It assigns the value returned by DateTime.Now to the d variable, not the property itself. Thus d will always equal d in that code.
But this will not always set result to true:
bool result = DateTime.Now == DateTime.Now;
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