简体   繁体   中英

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

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?

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

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;
}

The > operator is implemented as follows:

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

So it's really the same (as Ticks returns InternalTicks ).

The source shows this:

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

Which is

    internal Int64 InternalTicks {
        get {
            return (Int64)(dateData & TicksMask);
        }
    }

Where dateData is set in the constructor and simply refers to dateData = (UInt64)ticks; and TicksMask is

private const UInt64 TicksMask = 0x3FFFFFFFFFFFFFFF;

It is the same thing. From the source code the > operator is defined as:

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

Just for completeness, the Ticks property is defined as:

public long Ticks
{
        get 
        { 
            return InternalTicks; 
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM