Why use TimeSpan.CompareTo() rather than < > or =

Both internally does the same thing. Compare Ticks and return result.

public int CompareTo(TimeSpan value) {
    long t = value._ticks;
    if (_ticks > t) return 1;
    if (_ticks < t) return -1;
    return 0;
}

 public static bool operator <(TimeSpan t1, TimeSpan t2) {
            return t1._ticks < t2._ticks;
}

The only reason could be the other overload for CompareTo, which receives an object type parameter checks for null and then compare. Implemented like:

public int CompareTo(Object value) {
            if (value == null) return 1;
            if (!(value is TimeSpan))
                throw new ArgumentException(Environment.GetResourceString("Arg_MustBeTimeSpan"));
            long t = ((TimeSpan)value)._ticks;
            if (_ticks > t) return 1;
            if (_ticks < t) return -1;
            return 0;
        }

Source code from: Reference Source .NET Framework 4.5.1 – Microsoft

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)