Skip to content

Commit 5dab47d

Browse files
committed
Simplify some more CompareTo methods
1 parent 6647c6f commit 5dab47d

File tree

10 files changed

+16
-33
lines changed

10 files changed

+16
-33
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ public int CompareTo(object? value)
5858
{
5959
return 1;
6060
}
61-
if (!(value is byte))
61+
if (value is not byte other)
6262
{
6363
throw new ArgumentException(SR.Arg_MustBeByte);
6464
}
6565

66-
return m_value - (((byte)value).m_value);
66+
return CompareTo(other);
6767
}
6868

6969
public int CompareTo(byte value)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,12 @@ public int CompareTo(object? value)
140140
{
141141
return 1;
142142
}
143-
if (!(value is char))
143+
if (value is not char other)
144144
{
145145
throw new ArgumentException(SR.Arg_MustBeChar);
146146
}
147147

148-
return m_value - ((char)value).m_value;
148+
return CompareTo(other);
149149
}
150150

151151
public int CompareTo(char value)

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,11 +420,10 @@ public int CompareTo(object? value)
420420
{
421421
if (value == null)
422422
return 1;
423-
if (!(value is decimal))
423+
if (value is not decimal other)
424424
throw new ArgumentException(SR.Arg_MustBeDecimal);
425425

426-
decimal other = (decimal)value;
427-
return DecCalc.VarDecCmp(in this, in other);
426+
return CompareTo(other);
428427
}
429428

430429
public int CompareTo(decimal value)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ public int CompareTo(object? value)
6060
return 1;
6161
}
6262

63-
if (value is short)
63+
if (value is short other)
6464
{
65-
return m_value - ((short)value).m_value;
65+
return CompareTo(other);
6666
}
6767

6868
throw new ArgumentException(SR.Arg_MustBeInt16);

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,9 @@ public int CompareTo(object? value)
6363
return 1;
6464
}
6565

66-
// NOTE: Cannot use return (_value - value) as this causes a wrap
67-
// around in cases where _value - value > MaxValue.
6866
if (value is int i)
6967
{
70-
if (m_value < i) return -1;
71-
if (m_value > i) return 1;
72-
return 0;
68+
return CompareTo(i);
7369
}
7470

7571
throw new ArgumentException(SR.Arg_MustBeInt32);

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,9 @@ public int CompareTo(object? value)
6060
return 1;
6161
}
6262

63-
// Need to use compare because subtraction will wrap
64-
// to positive for very large neg numbers, etc.
6563
if (value is long i)
6664
{
67-
if (m_value < i) return -1;
68-
if (m_value > i) return 1;
69-
return 0;
65+
return CompareTo(i);
7066
}
7167

7268
throw new ArgumentException(SR.Arg_MustBeInt64);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ public int CompareTo(object? obj)
6262
{
6363
return 1;
6464
}
65-
if (!(obj is sbyte))
65+
if (obj is not sbyte other)
6666
{
6767
throw new ArgumentException(SR.Arg_MustBeSByte);
6868
}
69-
return m_value - ((sbyte)obj).m_value;
69+
return CompareTo(other);
7070
}
7171

7272
public int CompareTo(sbyte value)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ public int CompareTo(object? value)
5656
{
5757
return 1;
5858
}
59-
if (value is ushort)
59+
if (value is ushort other)
6060
{
61-
return (int)m_value - (int)(((ushort)value).m_value);
61+
return CompareTo(other);
6262
}
6363
throw new ArgumentException(SR.Arg_MustBeUInt16);
6464
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,9 @@ public int CompareTo(object? value)
5757
return 1;
5858
}
5959

60-
// Need to use compare because subtraction will wrap
61-
// to positive for very large neg numbers, etc.
6260
if (value is uint i)
6361
{
64-
if (m_value < i) return -1;
65-
if (m_value > i) return 1;
66-
return 0;
62+
return CompareTo(i);
6763
}
6864

6965
throw new ArgumentException(SR.Arg_MustBeUInt32);

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,9 @@ public int CompareTo(object? value)
5757
return 1;
5858
}
5959

60-
// Need to use compare because subtraction will wrap
61-
// to positive for very large neg numbers, etc.
6260
if (value is ulong i)
6361
{
64-
if (m_value < i) return -1;
65-
if (m_value > i) return 1;
66-
return 0;
62+
return CompareTo(i);
6763
}
6864

6965
throw new ArgumentException(SR.Arg_MustBeUInt64);

0 commit comments

Comments
 (0)