Skip to content

Commit 5cbc38c

Browse files
authored
Simplify some non-generic CompareTo methods (#101545)
* Simplify some CompareTo methods * Simplify some more CompareTo methods * Revert "Simplify some more CompareTo methods" This reverts commit 5dab47d. * Use DateTime.CompareTo in DatetimeOffset
1 parent 390973d commit 5cbc38c

File tree

3 files changed

+7
-86
lines changed

3 files changed

+7
-86
lines changed

src/libraries/System.Private.CoreLib/src/System/DateTimeOffset.cs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -571,26 +571,16 @@ public static int Compare(DateTimeOffset first, DateTimeOffset second) =>
571571
int IComparable.CompareTo(object? obj)
572572
{
573573
if (obj == null) return 1;
574-
if (!(obj is DateTimeOffset))
574+
if (obj is not DateTimeOffset other)
575575
{
576576
throw new ArgumentException(SR.Arg_MustBeDateTimeOffset);
577577
}
578578

579-
DateTime objUtc = ((DateTimeOffset)obj).UtcDateTime;
580-
DateTime utc = UtcDateTime;
581-
if (utc > objUtc) return 1;
582-
if (utc < objUtc) return -1;
583-
return 0;
579+
return DateTime.Compare(UtcDateTime, other.UtcDateTime);
584580
}
585581

586-
public int CompareTo(DateTimeOffset other)
587-
{
588-
DateTime otherUtc = other.UtcDateTime;
589-
DateTime utc = UtcDateTime;
590-
if (utc > otherUtc) return 1;
591-
if (utc < otherUtc) return -1;
592-
return 0;
593-
}
582+
public int CompareTo(DateTimeOffset other) =>
583+
DateTime.Compare(UtcDateTime, other.UtcDateTime);
594584

595585
// Checks if this DateTimeOffset is equal to a given object. Returns
596586
// true if the given object is a boxed DateTimeOffset and its value

src/libraries/System.Private.CoreLib/src/System/Guid.cs

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -958,68 +958,11 @@ public int CompareTo(object? value)
958958
{
959959
return 1;
960960
}
961-
if (!(value is Guid))
961+
if (value is not Guid other)
962962
{
963963
throw new ArgumentException(SR.Arg_MustBeGuid, nameof(value));
964964
}
965-
Guid g = (Guid)value;
966-
967-
if (g._a != _a)
968-
{
969-
return GetResult((uint)_a, (uint)g._a);
970-
}
971-
972-
if (g._b != _b)
973-
{
974-
return GetResult((uint)_b, (uint)g._b);
975-
}
976-
977-
if (g._c != _c)
978-
{
979-
return GetResult((uint)_c, (uint)g._c);
980-
}
981-
982-
if (g._d != _d)
983-
{
984-
return GetResult(_d, g._d);
985-
}
986-
987-
if (g._e != _e)
988-
{
989-
return GetResult(_e, g._e);
990-
}
991-
992-
if (g._f != _f)
993-
{
994-
return GetResult(_f, g._f);
995-
}
996-
997-
if (g._g != _g)
998-
{
999-
return GetResult(_g, g._g);
1000-
}
1001-
1002-
if (g._h != _h)
1003-
{
1004-
return GetResult(_h, g._h);
1005-
}
1006-
1007-
if (g._i != _i)
1008-
{
1009-
return GetResult(_i, g._i);
1010-
}
1011-
1012-
if (g._j != _j)
1013-
{
1014-
return GetResult(_j, g._j);
1015-
}
1016-
1017-
if (g._k != _k)
1018-
{
1019-
return GetResult(_k, g._k);
1020-
}
1021-
1022-
return 0;
965+
return CompareTo(other);
1023966
}
1024967

1025968
public int CompareTo(Guid value)

src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/NFloat.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -780,19 +780,7 @@ public int CompareTo(object? obj)
780780
{
781781
if (obj is NFloat other)
782782
{
783-
if (_value < other._value) return -1;
784-
if (_value > other._value) return 1;
785-
if (_value == other._value) return 0;
786-
787-
// At least one of the values is NaN.
788-
if (NativeType.IsNaN(_value))
789-
{
790-
return NativeType.IsNaN(other._value) ? 0 : -1;
791-
}
792-
else
793-
{
794-
return 1;
795-
}
783+
return CompareTo(other);
796784
}
797785
else if (obj is null)
798786
{

0 commit comments

Comments
 (0)