diff --git a/src/libraries/System.Private.CoreLib/src/System/DateTimeOffset.cs b/src/libraries/System.Private.CoreLib/src/System/DateTimeOffset.cs index 77f1153181f67f..5baf1e5076b6e9 100644 --- a/src/libraries/System.Private.CoreLib/src/System/DateTimeOffset.cs +++ b/src/libraries/System.Private.CoreLib/src/System/DateTimeOffset.cs @@ -571,26 +571,16 @@ public static int Compare(DateTimeOffset first, DateTimeOffset second) => int IComparable.CompareTo(object? obj) { if (obj == null) return 1; - if (!(obj is DateTimeOffset)) + if (obj is not DateTimeOffset other) { throw new ArgumentException(SR.Arg_MustBeDateTimeOffset); } - DateTime objUtc = ((DateTimeOffset)obj).UtcDateTime; - DateTime utc = UtcDateTime; - if (utc > objUtc) return 1; - if (utc < objUtc) return -1; - return 0; + return DateTime.Compare(UtcDateTime, other.UtcDateTime); } - public int CompareTo(DateTimeOffset other) - { - DateTime otherUtc = other.UtcDateTime; - DateTime utc = UtcDateTime; - if (utc > otherUtc) return 1; - if (utc < otherUtc) return -1; - return 0; - } + public int CompareTo(DateTimeOffset other) => + DateTime.Compare(UtcDateTime, other.UtcDateTime); // Checks if this DateTimeOffset is equal to a given object. Returns // true if the given object is a boxed DateTimeOffset and its value diff --git a/src/libraries/System.Private.CoreLib/src/System/Guid.cs b/src/libraries/System.Private.CoreLib/src/System/Guid.cs index 911fb9f7184a30..7ce5edfc3fbb84 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Guid.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Guid.cs @@ -958,68 +958,11 @@ public int CompareTo(object? value) { return 1; } - if (!(value is Guid)) + if (value is not Guid other) { throw new ArgumentException(SR.Arg_MustBeGuid, nameof(value)); } - Guid g = (Guid)value; - - if (g._a != _a) - { - return GetResult((uint)_a, (uint)g._a); - } - - if (g._b != _b) - { - return GetResult((uint)_b, (uint)g._b); - } - - if (g._c != _c) - { - return GetResult((uint)_c, (uint)g._c); - } - - if (g._d != _d) - { - return GetResult(_d, g._d); - } - - if (g._e != _e) - { - return GetResult(_e, g._e); - } - - if (g._f != _f) - { - return GetResult(_f, g._f); - } - - if (g._g != _g) - { - return GetResult(_g, g._g); - } - - if (g._h != _h) - { - return GetResult(_h, g._h); - } - - if (g._i != _i) - { - return GetResult(_i, g._i); - } - - if (g._j != _j) - { - return GetResult(_j, g._j); - } - - if (g._k != _k) - { - return GetResult(_k, g._k); - } - - return 0; + return CompareTo(other); } public int CompareTo(Guid value) diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/NFloat.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/NFloat.cs index b314daec02a93c..125ee553010663 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/NFloat.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/NFloat.cs @@ -780,19 +780,7 @@ public int CompareTo(object? obj) { if (obj is NFloat other) { - if (_value < other._value) return -1; - if (_value > other._value) return 1; - if (_value == other._value) return 0; - - // At least one of the values is NaN. - if (NativeType.IsNaN(_value)) - { - return NativeType.IsNaN(other._value) ? 0 : -1; - } - else - { - return 1; - } + return CompareTo(other); } else if (obj is null) {