Update ValueTuple.cs with C# 9 pattern matching (#44828)
authorEirik Tsarpalis <eirik.tsarpalis@gmail.com>
Mon, 18 Jan 2021 20:36:27 +0000 (20:36 +0000)
committerGitHub <noreply@github.com>
Mon, 18 Jan 2021 20:36:27 +0000 (20:36 +0000)
* use C# 9 constructs in ValueTuple.cs

* address feedback

* address feedback

* address feedback

* address feedback

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

index ad92422..73134ea 100644 (file)
@@ -58,9 +58,9 @@ namespace System
 
         int IComparable.CompareTo(object? other)
         {
-            if (other == null) return 1;
+            if (other is null) return 1;
 
-            if (!(other is ValueTuple))
+            if (other is not ValueTuple)
             {
                 ThrowHelper.ThrowArgumentException_TupleIncorrectType(this);
             }
@@ -83,9 +83,9 @@ namespace System
 
         int IStructuralComparable.CompareTo(object? other, IComparer comparer)
         {
-            if (other == null) return 1;
+            if (other is null) return 1;
 
-            if (!(other is ValueTuple))
+            if (other is not ValueTuple)
             {
                 ThrowHelper.ThrowArgumentException_TupleIncorrectType(this);
             }
@@ -291,7 +291,7 @@ namespace System
         /// </remarks>
         public override bool Equals(object? obj)
         {
-            return obj is ValueTuple<T1> && Equals((ValueTuple<T1>)obj);
+            return obj is ValueTuple<T1> tuple && Equals(tuple);
         }
 
         /// <summary>
@@ -309,27 +309,23 @@ namespace System
             return EqualityComparer<T1>.Default.Equals(Item1, other.Item1);
         }
 
-        bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer)
-        {
-            if (other == null || !(other is ValueTuple<T1>)) return false;
-
-            var objTuple = (ValueTuple<T1>)other;
-
-            return comparer.Equals(Item1, objTuple.Item1);
-        }
+        bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer) =>
+            other is ValueTuple<T1> vt &&
+            comparer.Equals(Item1, vt.Item1);
 
         int IComparable.CompareTo(object? other)
         {
-            if (other == null) return 1;
-
-            if (!(other is ValueTuple<T1>))
+            if (other is not null)
             {
+                if (other is ValueTuple<T1> objTuple)
+                {
+                    return Comparer<T1>.Default.Compare(Item1, objTuple.Item1);
+                }
+
                 ThrowHelper.ThrowArgumentException_TupleIncorrectType(this);
             }
 
-            var objTuple = (ValueTuple<T1>)other;
-
-            return Comparer<T1>.Default.Compare(Item1, objTuple.Item1);
+            return 1;
         }
 
         /// <summary>Compares this instance to a specified instance and returns an indication of their relative values.</summary>
@@ -347,16 +343,17 @@ namespace System
 
         int IStructuralComparable.CompareTo(object? other, IComparer comparer)
         {
-            if (other == null) return 1;
-
-            if (!(other is ValueTuple<T1>))
+            if (other is not null)
             {
+                if (other is ValueTuple<T1> objTuple)
+                {
+                    return comparer.Compare(Item1, objTuple.Item1);
+                }
+
                 ThrowHelper.ThrowArgumentException_TupleIncorrectType(this);
             }
 
-            var objTuple = (ValueTuple<T1>)other;
-
-            return comparer.Compare(Item1, objTuple.Item1);
+            return 1;
         }
 
         /// <summary>
@@ -466,7 +463,7 @@ namespace System
         /// </remarks>
         public override bool Equals(object? obj)
         {
-            return obj is ValueTuple<T1, T2> && Equals((ValueTuple<T1, T2>)obj);
+            return obj is ValueTuple<T1, T2> tuple && Equals(tuple);
         }
 
         /// <summary>
@@ -502,26 +499,24 @@ namespace System
         ///  <see cref="IEqualityComparer.Equals"/> implementation. If this method call returns <see langword="true"/>, the method is
         ///  called again and passed the <see cref="Item2"/> values of the two <see cref="ValueTuple{T1, T2}"/> instances.
         /// </remarks>
-        bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer)
-        {
-            if (other == null || !(other is ValueTuple<T1, T2>)) return false;
-
-            var objTuple = (ValueTuple<T1, T2>)other;
-
-            return comparer.Equals(Item1, objTuple.Item1)
-                && comparer.Equals(Item2, objTuple.Item2);
-        }
+        bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer) =>
+            other is ValueTuple<T1, T2> vt &&
+            comparer.Equals(Item1, vt.Item1) &&
+            comparer.Equals(Item2, vt.Item2);
 
         int IComparable.CompareTo(object? other)
         {
-            if (other == null) return 1;
-
-            if (!(other is ValueTuple<T1, T2>))
+            if (other is not null)
             {
+                if (other is ValueTuple<T1, T2> objTuple)
+                {
+                    return CompareTo(objTuple);
+                }
+
                 ThrowHelper.ThrowArgumentException_TupleIncorrectType(this);
             }
 
-            return CompareTo((ValueTuple<T1, T2>)other);
+            return 1;
         }
 
         /// <summary>Compares this instance to a specified instance and returns an indication of their relative values.</summary>
@@ -542,19 +537,20 @@ namespace System
 
         int IStructuralComparable.CompareTo(object? other, IComparer comparer)
         {
-            if (other == null) return 1;
-
-            if (!(other is ValueTuple<T1, T2>))
+            if (other is not null)
             {
-                ThrowHelper.ThrowArgumentException_TupleIncorrectType(this);
-            }
+                if (other is ValueTuple<T1, T2> objTuple)
+                {
+                    int c = comparer.Compare(Item1, objTuple.Item1);
+                    if (c != 0) return c;
 
-            var objTuple = (ValueTuple<T1, T2>)other;
+                    return comparer.Compare(Item2, objTuple.Item2);
+                }
 
-            int c = comparer.Compare(Item1, objTuple.Item1);
-            if (c != 0) return c;
+                ThrowHelper.ThrowArgumentException_TupleIncorrectType(this);
+            }
 
-            return comparer.Compare(Item2, objTuple.Item2);
+            return 1;
         }
 
         /// <summary>
@@ -673,7 +669,7 @@ namespace System
         /// </remarks>
         public override bool Equals(object? obj)
         {
-            return obj is ValueTuple<T1, T2, T3> && Equals((ValueTuple<T1, T2, T3>)obj);
+            return obj is ValueTuple<T1, T2, T3> tuple && Equals(tuple);
         }
 
         /// <summary>
@@ -693,27 +689,25 @@ namespace System
                 && EqualityComparer<T3>.Default.Equals(Item3, other.Item3);
         }
 
-        bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer)
-        {
-            if (other == null || !(other is ValueTuple<T1, T2, T3>)) return false;
-
-            var objTuple = (ValueTuple<T1, T2, T3>)other;
-
-            return comparer.Equals(Item1, objTuple.Item1)
-                && comparer.Equals(Item2, objTuple.Item2)
-                && comparer.Equals(Item3, objTuple.Item3);
-        }
+        bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer) =>
+            other is ValueTuple<T1, T2, T3> vt &&
+            comparer.Equals(Item1, vt.Item1) &&
+            comparer.Equals(Item2, vt.Item2) &&
+            comparer.Equals(Item3, vt.Item3);
 
         int IComparable.CompareTo(object? other)
         {
-            if (other == null) return 1;
-
-            if (!(other is ValueTuple<T1, T2, T3>))
+            if (other is not null)
             {
+                if (other is ValueTuple<T1, T2, T3> objTuple)
+                {
+                    return CompareTo(objTuple);
+                }
+
                 ThrowHelper.ThrowArgumentException_TupleIncorrectType(this);
             }
 
-            return CompareTo((ValueTuple<T1, T2, T3>)other);
+            return 1;
         }
 
         /// <summary>Compares this instance to a specified instance and returns an indication of their relative values.</summary>
@@ -737,22 +731,23 @@ namespace System
 
         int IStructuralComparable.CompareTo(object? other, IComparer comparer)
         {
-            if (other == null) return 1;
-
-            if (!(other is ValueTuple<T1, T2, T3>))
+            if (other is not null)
             {
-                ThrowHelper.ThrowArgumentException_TupleIncorrectType(this);
-            }
+                if (other is ValueTuple<T1, T2, T3> objTuple)
+                {
+                    int c = comparer.Compare(Item1, objTuple.Item1);
+                    if (c != 0) return c;
 
-            var objTuple = (ValueTuple<T1, T2, T3>)other;
+                    c = comparer.Compare(Item2, objTuple.Item2);
+                    if (c != 0) return c;
 
-            int c = comparer.Compare(Item1, objTuple.Item1);
-            if (c != 0) return c;
+                    return comparer.Compare(Item3, objTuple.Item3);
+                }
 
-            c = comparer.Compare(Item2, objTuple.Item2);
-            if (c != 0) return c;
+                ThrowHelper.ThrowArgumentException_TupleIncorrectType(this);
+            }
 
-            return comparer.Compare(Item3, objTuple.Item3);
+            return 1;
         }
 
         /// <summary>
@@ -879,7 +874,7 @@ namespace System
         /// </remarks>
         public override bool Equals(object? obj)
         {
-            return obj is ValueTuple<T1, T2, T3, T4> && Equals((ValueTuple<T1, T2, T3, T4>)obj);
+            return obj is ValueTuple<T1, T2, T3, T4> tuple && Equals(tuple);
         }
 
         /// <summary>
@@ -900,28 +895,26 @@ namespace System
                 && EqualityComparer<T4>.Default.Equals(Item4, other.Item4);
         }
 
-        bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer)
-        {
-            if (other == null || !(other is ValueTuple<T1, T2, T3, T4>)) return false;
-
-            var objTuple = (ValueTuple<T1, T2, T3, T4>)other;
-
-            return comparer.Equals(Item1, objTuple.Item1)
-                && comparer.Equals(Item2, objTuple.Item2)
-                && comparer.Equals(Item3, objTuple.Item3)
-                && comparer.Equals(Item4, objTuple.Item4);
-        }
+        bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer) =>
+            other is ValueTuple<T1, T2, T3, T4> vt &&
+            comparer.Equals(Item1, vt.Item1) &&
+            comparer.Equals(Item2, vt.Item2) &&
+            comparer.Equals(Item3, vt.Item3) &&
+            comparer.Equals(Item4, vt.Item4);
 
         int IComparable.CompareTo(object? other)
         {
-            if (other == null) return 1;
-
-            if (!(other is ValueTuple<T1, T2, T3, T4>))
+            if (other is not null)
             {
+                if (other is ValueTuple<T1, T2, T3, T4> objTuple)
+                {
+                    return CompareTo(objTuple);
+                }
+
                 ThrowHelper.ThrowArgumentException_TupleIncorrectType(this);
             }
 
-            return CompareTo((ValueTuple<T1, T2, T3, T4>)other);
+            return 1;
         }
 
         /// <summary>Compares this instance to a specified instance and returns an indication of their relative values.</summary>
@@ -948,25 +941,26 @@ namespace System
 
         int IStructuralComparable.CompareTo(object? other, IComparer comparer)
         {
-            if (other == null) return 1;
-
-            if (!(other is ValueTuple<T1, T2, T3, T4>))
+            if (other is not null)
             {
-                ThrowHelper.ThrowArgumentException_TupleIncorrectType(this);
-            }
+                if (other is ValueTuple<T1, T2, T3, T4> objTuple)
+                {
+                    int c = comparer.Compare(Item1, objTuple.Item1);
+                    if (c != 0) return c;
 
-            var objTuple = (ValueTuple<T1, T2, T3, T4>)other;
+                    c = comparer.Compare(Item2, objTuple.Item2);
+                    if (c != 0) return c;
 
-            int c = comparer.Compare(Item1, objTuple.Item1);
-            if (c != 0) return c;
+                    c = comparer.Compare(Item3, objTuple.Item3);
+                    if (c != 0) return c;
 
-            c = comparer.Compare(Item2, objTuple.Item2);
-            if (c != 0) return c;
+                    return comparer.Compare(Item4, objTuple.Item4);
+                }
 
-            c = comparer.Compare(Item3, objTuple.Item3);
-            if (c != 0) return c;
+                ThrowHelper.ThrowArgumentException_TupleIncorrectType(this);
+            }
 
-            return comparer.Compare(Item4, objTuple.Item4);
+            return 1;
         }
 
         /// <summary>
@@ -1103,7 +1097,7 @@ namespace System
         /// </remarks>
         public override bool Equals(object? obj)
         {
-            return obj is ValueTuple<T1, T2, T3, T4, T5> && Equals((ValueTuple<T1, T2, T3, T4, T5>)obj);
+            return obj is ValueTuple<T1, T2, T3, T4, T5> tuple && Equals(tuple);
         }
 
         /// <summary>
@@ -1125,29 +1119,26 @@ namespace System
                 && EqualityComparer<T5>.Default.Equals(Item5, other.Item5);
         }
 
-        bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer)
-        {
-            if (other == null || !(other is ValueTuple<T1, T2, T3, T4, T5>)) return false;
-
-            var objTuple = (ValueTuple<T1, T2, T3, T4, T5>)other;
-
-            return comparer.Equals(Item1, objTuple.Item1)
-                && comparer.Equals(Item2, objTuple.Item2)
-                && comparer.Equals(Item3, objTuple.Item3)
-                && comparer.Equals(Item4, objTuple.Item4)
-                && comparer.Equals(Item5, objTuple.Item5);
-        }
+        bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer) =>
+            other is ValueTuple<T1, T2, T3, T4, T5> vt &&
+            comparer.Equals(Item1, vt.Item1) &&
+            comparer.Equals(Item2, vt.Item2) &&
+            comparer.Equals(Item3, vt.Item3) &&
+            comparer.Equals(Item5, vt.Item5);
 
         int IComparable.CompareTo(object? other)
         {
-            if (other == null) return 1;
-
-            if (!(other is ValueTuple<T1, T2, T3, T4, T5>))
+            if (other is not null)
             {
+                if (other is ValueTuple<T1, T2, T3, T4, T5> objTuple)
+                {
+                    return CompareTo(objTuple);
+                }
+
                 ThrowHelper.ThrowArgumentException_TupleIncorrectType(this);
             }
 
-            return CompareTo((ValueTuple<T1, T2, T3, T4, T5>)other);
+            return 1;
         }
 
         /// <summary>Compares this instance to a specified instance and returns an indication of their relative values.</summary>
@@ -1177,28 +1168,29 @@ namespace System
 
         int IStructuralComparable.CompareTo(object? other, IComparer comparer)
         {
-            if (other == null) return 1;
-
-            if (!(other is ValueTuple<T1, T2, T3, T4, T5>))
+            if (other is not null)
             {
-                ThrowHelper.ThrowArgumentException_TupleIncorrectType(this);
-            }
+                if (other is ValueTuple<T1, T2, T3, T4, T5> objTuple)
+                {
+                    int c = comparer.Compare(Item1, objTuple.Item1);
+                    if (c != 0) return c;
 
-            var objTuple = (ValueTuple<T1, T2, T3, T4, T5>)other;
+                    c = comparer.Compare(Item2, objTuple.Item2);
+                    if (c != 0) return c;
 
-            int c = comparer.Compare(Item1, objTuple.Item1);
-            if (c != 0) return c;
+                    c = comparer.Compare(Item3, objTuple.Item3);
+                    if (c != 0) return c;
 
-            c = comparer.Compare(Item2, objTuple.Item2);
-            if (c != 0) return c;
+                    c = comparer.Compare(Item4, objTuple.Item4);
+                    if (c != 0) return c;
 
-            c = comparer.Compare(Item3, objTuple.Item3);
-            if (c != 0) return c;
+                    return comparer.Compare(Item5, objTuple.Item5);
+                }
 
-            c = comparer.Compare(Item4, objTuple.Item4);
-            if (c != 0) return c;
+                ThrowHelper.ThrowArgumentException_TupleIncorrectType(this);
+            }
 
-            return comparer.Compare(Item5, objTuple.Item5);
+            return 1;
         }
 
         /// <summary>
@@ -1345,7 +1337,7 @@ namespace System
         /// </remarks>
         public override bool Equals(object? obj)
         {
-            return obj is ValueTuple<T1, T2, T3, T4, T5, T6> && Equals((ValueTuple<T1, T2, T3, T4, T5, T6>)obj);
+            return obj is ValueTuple<T1, T2, T3, T4, T5, T6> tuple && Equals(tuple);
         }
 
         /// <summary>
@@ -1368,30 +1360,27 @@ namespace System
                 && EqualityComparer<T6>.Default.Equals(Item6, other.Item6);
         }
 
-        bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer)
-        {
-            if (other == null || !(other is ValueTuple<T1, T2, T3, T4, T5, T6>)) return false;
-
-            var objTuple = (ValueTuple<T1, T2, T3, T4, T5, T6>)other;
-
-            return comparer.Equals(Item1, objTuple.Item1)
-                && comparer.Equals(Item2, objTuple.Item2)
-                && comparer.Equals(Item3, objTuple.Item3)
-                && comparer.Equals(Item4, objTuple.Item4)
-                && comparer.Equals(Item5, objTuple.Item5)
-                && comparer.Equals(Item6, objTuple.Item6);
-        }
+        bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer) =>
+            other is ValueTuple<T1, T2, T3, T4, T5, T6> vt &&
+            comparer.Equals(Item1, vt.Item1) &&
+            comparer.Equals(Item2, vt.Item2) &&
+            comparer.Equals(Item3, vt.Item3) &&
+            comparer.Equals(Item5, vt.Item5) &&
+            comparer.Equals(Item6, vt.Item6);
 
         int IComparable.CompareTo(object? other)
         {
-            if (other == null) return 1;
-
-            if (!(other is ValueTuple<T1, T2, T3, T4, T5, T6>))
+            if (other is not null)
             {
+                if (other is ValueTuple<T1, T2, T3, T4, T5, T6> objTuple)
+                {
+                    return CompareTo(objTuple);
+                }
+
                 ThrowHelper.ThrowArgumentException_TupleIncorrectType(this);
             }
 
-            return CompareTo((ValueTuple<T1, T2, T3, T4, T5, T6>)other);
+            return 1;
         }
 
         /// <summary>Compares this instance to a specified instance and returns an indication of their relative values.</summary>
@@ -1424,31 +1413,32 @@ namespace System
 
         int IStructuralComparable.CompareTo(object? other, IComparer comparer)
         {
-            if (other == null) return 1;
-
-            if (!(other is ValueTuple<T1, T2, T3, T4, T5, T6>))
+            if (other is not null)
             {
-                ThrowHelper.ThrowArgumentException_TupleIncorrectType(this);
-            }
+                if (other is ValueTuple<T1, T2, T3, T4, T5, T6> objTuple)
+                {
+                    int c = comparer.Compare(Item1, objTuple.Item1);
+                    if (c != 0) return c;
 
-            var objTuple = (ValueTuple<T1, T2, T3, T4, T5, T6>)other;
+                    c = comparer.Compare(Item2, objTuple.Item2);
+                    if (c != 0) return c;
 
-            int c = comparer.Compare(Item1, objTuple.Item1);
-            if (c != 0) return c;
+                    c = comparer.Compare(Item3, objTuple.Item3);
+                    if (c != 0) return c;
 
-            c = comparer.Compare(Item2, objTuple.Item2);
-            if (c != 0) return c;
+                    c = comparer.Compare(Item4, objTuple.Item4);
+                    if (c != 0) return c;
 
-            c = comparer.Compare(Item3, objTuple.Item3);
-            if (c != 0) return c;
+                    c = comparer.Compare(Item5, objTuple.Item5);
+                    if (c != 0) return c;
 
-            c = comparer.Compare(Item4, objTuple.Item4);
-            if (c != 0) return c;
+                    return comparer.Compare(Item6, objTuple.Item6);
+                }
 
-            c = comparer.Compare(Item5, objTuple.Item5);
-            if (c != 0) return c;
+                ThrowHelper.ThrowArgumentException_TupleIncorrectType(this);
+            }
 
-            return comparer.Compare(Item6, objTuple.Item6);
+            return 1;
         }
 
         /// <summary>
@@ -1605,7 +1595,7 @@ namespace System
         /// </remarks>
         public override bool Equals(object? obj)
         {
-            return obj is ValueTuple<T1, T2, T3, T4, T5, T6, T7> && Equals((ValueTuple<T1, T2, T3, T4, T5, T6, T7>)obj);
+            return obj is ValueTuple<T1, T2, T3, T4, T5, T6, T7> tuple && Equals(tuple);
         }
 
         /// <summary>
@@ -1629,31 +1619,28 @@ namespace System
                 && EqualityComparer<T7>.Default.Equals(Item7, other.Item7);
         }
 
-        bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer)
-        {
-            if (other == null || !(other is ValueTuple<T1, T2, T3, T4, T5, T6, T7>)) return false;
-
-            var objTuple = (ValueTuple<T1, T2, T3, T4, T5, T6, T7>)other;
-
-            return comparer.Equals(Item1, objTuple.Item1)
-                && comparer.Equals(Item2, objTuple.Item2)
-                && comparer.Equals(Item3, objTuple.Item3)
-                && comparer.Equals(Item4, objTuple.Item4)
-                && comparer.Equals(Item5, objTuple.Item5)
-                && comparer.Equals(Item6, objTuple.Item6)
-                && comparer.Equals(Item7, objTuple.Item7);
-        }
+        bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer) =>
+            other is ValueTuple<T1, T2, T3, T4, T5, T6, T7> vt &&
+            comparer.Equals(Item1, vt.Item1) &&
+            comparer.Equals(Item2, vt.Item2) &&
+            comparer.Equals(Item3, vt.Item3) &&
+            comparer.Equals(Item5, vt.Item5) &&
+            comparer.Equals(Item6, vt.Item6) &&
+            comparer.Equals(Item7, vt.Item7);
 
         int IComparable.CompareTo(object? other)
         {
-            if (other == null) return 1;
-
-            if (!(other is ValueTuple<T1, T2, T3, T4, T5, T6, T7>))
+            if (other is not null)
             {
+                if (other is ValueTuple<T1, T2, T3, T4, T5, T6, T7> objTuple)
+                {
+                    return CompareTo(objTuple);
+                }
+
                 ThrowHelper.ThrowArgumentException_TupleIncorrectType(this);
             }
 
-            return CompareTo((ValueTuple<T1, T2, T3, T4, T5, T6, T7>)other);
+            return 1;
         }
 
         /// <summary>Compares this instance to a specified instance and returns an indication of their relative values.</summary>
@@ -1689,34 +1676,35 @@ namespace System
 
         int IStructuralComparable.CompareTo(object? other, IComparer comparer)
         {
-            if (other == null) return 1;
-
-            if (!(other is ValueTuple<T1, T2, T3, T4, T5, T6, T7>))
+            if (other is not null)
             {
-                ThrowHelper.ThrowArgumentException_TupleIncorrectType(this);
-            }
+                if (other is ValueTuple<T1, T2, T3, T4, T5, T6, T7> objTuple)
+                {
+                    int c = comparer.Compare(Item1, objTuple.Item1);
+                    if (c != 0) return c;
 
-            var objTuple = (ValueTuple<T1, T2, T3, T4, T5, T6, T7>)other;
+                    c = comparer.Compare(Item2, objTuple.Item2);
+                    if (c != 0) return c;
 
-            int c = comparer.Compare(Item1, objTuple.Item1);
-            if (c != 0) return c;
+                    c = comparer.Compare(Item3, objTuple.Item3);
+                    if (c != 0) return c;
 
-            c = comparer.Compare(Item2, objTuple.Item2);
-            if (c != 0) return c;
+                    c = comparer.Compare(Item4, objTuple.Item4);
+                    if (c != 0) return c;
 
-            c = comparer.Compare(Item3, objTuple.Item3);
-            if (c != 0) return c;
+                    c = comparer.Compare(Item5, objTuple.Item5);
+                    if (c != 0) return c;
 
-            c = comparer.Compare(Item4, objTuple.Item4);
-            if (c != 0) return c;
+                    c = comparer.Compare(Item6, objTuple.Item6);
+                    if (c != 0) return c;
 
-            c = comparer.Compare(Item5, objTuple.Item5);
-            if (c != 0) return c;
+                    return comparer.Compare(Item7, objTuple.Item7);
+                }
 
-            c = comparer.Compare(Item6, objTuple.Item6);
-            if (c != 0) return c;
+                ThrowHelper.ThrowArgumentException_TupleIncorrectType(this);
+            }
 
-            return comparer.Compare(Item7, objTuple.Item7);
+            return 1;
         }
 
         /// <summary>
@@ -1859,7 +1847,7 @@ namespace System
         /// <param name="rest">The value of the tuple's eight component.</param>
         public ValueTuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7, TRest rest)
         {
-            if (!(rest is IValueTupleInternal))
+            if (rest is not IValueTupleInternal)
             {
                 throw new ArgumentException(SR.ArgumentException_ValueTupleLastArgumentNotAValueTuple);
             }
@@ -1889,7 +1877,7 @@ namespace System
         /// </remarks>
         public override bool Equals(object? obj)
         {
-            return obj is ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> && Equals((ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>)obj);
+            return obj is ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> tuple && Equals(tuple);
         }
 
         /// <summary>
@@ -1914,32 +1902,29 @@ namespace System
                 && EqualityComparer<TRest>.Default.Equals(Rest, other.Rest);
         }
 
-        bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer)
-        {
-            if (other == null || !(other is ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>)) return false;
-
-            var objTuple = (ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>)other;
-
-            return comparer.Equals(Item1, objTuple.Item1)
-                && comparer.Equals(Item2, objTuple.Item2)
-                && comparer.Equals(Item3, objTuple.Item3)
-                && comparer.Equals(Item4, objTuple.Item4)
-                && comparer.Equals(Item5, objTuple.Item5)
-                && comparer.Equals(Item6, objTuple.Item6)
-                && comparer.Equals(Item7, objTuple.Item7)
-                && comparer.Equals(Rest, objTuple.Rest);
-        }
+        bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer) =>
+            other is ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> vt &&
+            comparer.Equals(Item1, vt.Item1) &&
+            comparer.Equals(Item2, vt.Item2) &&
+            comparer.Equals(Item3, vt.Item3) &&
+            comparer.Equals(Item5, vt.Item5) &&
+            comparer.Equals(Item6, vt.Item6) &&
+            comparer.Equals(Item7, vt.Item7) &&
+            comparer.Equals(Rest, vt.Rest);
 
         int IComparable.CompareTo(object? other)
         {
-            if (other == null) return 1;
-
-            if (!(other is ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>))
+            if (other is not null)
             {
+                if (other is ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> objTuple)
+                {
+                    return CompareTo(objTuple);
+                }
+
                 ThrowHelper.ThrowArgumentException_TupleIncorrectType(this);
             }
 
-            return CompareTo((ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>)other);
+            return 1;
         }
 
         /// <summary>Compares this instance to a specified instance and returns an indication of their relative values.</summary>
@@ -1978,37 +1963,38 @@ namespace System
 
         int IStructuralComparable.CompareTo(object? other, IComparer comparer)
         {
-            if (other == null) return 1;
-
-            if (!(other is ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>))
+            if (other is not null)
             {
-                ThrowHelper.ThrowArgumentException_TupleIncorrectType(this);
-            }
+                if (other is ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> objTuple)
+                {
+                    int c = comparer.Compare(Item1, objTuple.Item1);
+                    if (c != 0) return c;
 
-            var objTuple = (ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>)other;
+                    c = comparer.Compare(Item2, objTuple.Item2);
+                    if (c != 0) return c;
 
-            int c = comparer.Compare(Item1, objTuple.Item1);
-            if (c != 0) return c;
+                    c = comparer.Compare(Item3, objTuple.Item3);
+                    if (c != 0) return c;
 
-            c = comparer.Compare(Item2, objTuple.Item2);
-            if (c != 0) return c;
+                    c = comparer.Compare(Item4, objTuple.Item4);
+                    if (c != 0) return c;
 
-            c = comparer.Compare(Item3, objTuple.Item3);
-            if (c != 0) return c;
+                    c = comparer.Compare(Item5, objTuple.Item5);
+                    if (c != 0) return c;
 
-            c = comparer.Compare(Item4, objTuple.Item4);
-            if (c != 0) return c;
+                    c = comparer.Compare(Item6, objTuple.Item6);
+                    if (c != 0) return c;
 
-            c = comparer.Compare(Item5, objTuple.Item5);
-            if (c != 0) return c;
+                    c = comparer.Compare(Item7, objTuple.Item7);
+                    if (c != 0) return c;
 
-            c = comparer.Compare(Item6, objTuple.Item6);
-            if (c != 0) return c;
+                    return comparer.Compare(Rest, objTuple.Rest);
+                }
 
-            c = comparer.Compare(Item7, objTuple.Item7);
-            if (c != 0) return c;
+                ThrowHelper.ThrowArgumentException_TupleIncorrectType(this);
+            }
 
-            return comparer.Compare(Rest, objTuple.Rest);
+            return 1;
         }
 
         /// <summary>
@@ -2018,7 +2004,7 @@ namespace System
         public override int GetHashCode()
         {
             // We want to have a limited hash in this case. We'll use the first 7 elements of the tuple
-            if (!(Rest is IValueTupleInternal))
+            if (Rest is not IValueTupleInternal)
             {
                 return HashCode.Combine(Item1?.GetHashCode() ?? 0,
                                         Item2?.GetHashCode() ?? 0,
@@ -2097,7 +2083,7 @@ namespace System
         private int GetHashCodeCore(IEqualityComparer comparer)
         {
             // We want to have a limited hash in this case. We'll use the first 7 elements of the tuple
-            if (!(Rest is IValueTupleInternal rest))
+            if (Rest is not IValueTupleInternal rest)
             {
                 return HashCode.Combine(comparer.GetHashCode(Item1!), comparer.GetHashCode(Item2!), comparer.GetHashCode(Item3!),
                                         comparer.GetHashCode(Item4!), comparer.GetHashCode(Item5!), comparer.GetHashCode(Item6!),