Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C#, what is the difference between comparing two dates using tick and just as it is

Tags:

c#

.net

datetime

I'm new to C#. I was going through some code written by someone who worked on my project earlier when I came across this:

if (olderTime.happenedWhen.Ticks > happenedWhen.Ticks) {    thisIsTrulyNew = false; } 

Both olderTime.happenedWhen and happenedWhen are of type DateTime.

Is this a more accurate way of comparing DateTime?

I know that Ticks represents 100 nano-second intervals from 00:00, January 1, 0001. But why do this sort of a comparison when I thought we could do:

if (olderTime.happenedWhen > happenedWhen){    thisIsTrulyNew = false } 

Does the ticks comparison achieve something that the normal comparison wouldn't?

like image 583
Eakan Gopalakrishnan Avatar asked Jun 03 '14 13:06

Eakan Gopalakrishnan


People also ask

What does |= mean in C?

The ' |= ' symbol is the bitwise OR assignment operator.

What is '~' in C programming?

In mathematics, the tilde often represents approximation, especially when used in duplicate, and is sometimes called the "equivalency sign." In regular expressions, the tilde is used as an operator in pattern matching, and in C programming, it is used as a bitwise operator representing a unary negation (i.e., "bitwise ...

What is operators in C?

C operators are one of the features in C which has symbols that can be used to perform mathematical, relational, bitwise, conditional, or logical manipulations. The C programming language has a lot of built-in operators to perform various tasks as per the need of the program.

What is the use of in C?

In C/C++, the # sign marks preprocessor directives. If you're not familiar with the preprocessor, it works as part of the compilation process, handling includes, macros, and more.


2 Answers

Is this a more accurate way of comparing DateTime?

Not in the slightest. In fact, that is how the > operator is implemented internally.

From the .NET Reference source:

public static bool operator >(DateTime t1, DateTime t2) {     return t1.InternalTicks > t2.InternalTicks; } 

Someone might have thought they were being clever by skipping the one line of internal code and going straight to the Ticks property. In reality, the getter for Ticks returns InternalTicks, so unless it is optimized by the compiler, using the Ticks property adds two calls in order to save one call (neither of which would change the performance significantly).

like image 157
D Stanley Avatar answered Sep 29 '22 10:09

D Stanley


The implementation of the operator > for DateTime compares the ticks too, as you can see from this disassembled code (mscorlib.dll, System.DateTime class):

[__DynamicallyInvokable] public long Ticks {     [__DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]     get     {         return this.InternalTicks;     } }  [__DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")] public static bool operator >(DateTime t1, DateTime t2) {     return t1.InternalTicks > t2.InternalTicks; } 
like image 40
Dirk Avatar answered Sep 29 '22 12:09

Dirk