Repository: dotnet/runtime
File: src/libraries/System.Collections.Immutable/src/System/Linq/ImmutableArrayExtensions.cs (C#)

Code before:

            for (int i = 0; i < immutableArray.Length; i++)
            {
                if (!comparer.Equals(immutableArray.array![i], items.array![i]))
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// Determines whether two sequences are equal according to an equality comparer.
        /// </summary>
        /// <typeparam name="TDerived">The type of element in the compared array.</typeparam>
        /// <typeparam name="TBase">The type of element contained by the collection.</typeparam>
        public static bool SequenceEqual<TDerived, TBase>(this ImmutableArray<TBase> immutableArray, IEnumerable<TDerived> items, IEqualityComparer<TBase>? comparer = null) where TDerived : TBase
        {
            if (items is ICollection<TBase> itemsCol)
            {
                immutableArray.ThrowNullRefIfNotInitialized();
                return Enumerable.SequenceEqual(immutableArray.array!, itemsCol, comparer);
            }

            return Impl(immutableArray, items, comparer);

            static bool Impl(ImmutableArray<TBase> immutableArray, IEnumerable<TDerived> items, IEqualityComparer<TBase>? comparer)
            {
                Requires.NotNull(items, nameof(items));

                comparer ??= EqualityComparer<TBase>.Default;

                int i = 0;
                int n = immutableArray.Length;
                foreach (TDerived item in items)
                {
                    if (i == n)
                    {
                        return false;
                    }

                    if (!comparer.Equals(immutableArray[i], item))
                    {
                        return false;
                    }

                    i++;
                }

                return i == n;

Review comment (suggestion):
If you're making more changes, may as well change the method name to `Enumerate` to be more descriptive.

Code after:

            for (int i = 0; i < immutableArray.Length; i++)
            {
                if (!comparer.Equals(immutableArray.array[i], items.array[i]))
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// Determines whether two sequences are equal according to an equality comparer.
        /// </summary>
        /// <typeparam name="TDerived">The type of element in the compared array.</typeparam>
        /// <typeparam name="TBase">The type of element contained by the collection.</typeparam>
        public static bool SequenceEqual<TDerived, TBase>(this ImmutableArray<TBase> immutableArray, IEnumerable<TDerived> items, IEqualityComparer<TBase>? comparer = null) where TDerived : TBase
        {
            if (items is ICollection<TBase> itemsCol)
            {
                immutableArray.ThrowNullRefIfNotInitialized();
                return Enumerable.SequenceEqual(immutableArray.array, itemsCol, comparer);
            }

            return Enumerate(immutableArray, items, comparer);

            static bool Enumerate(ImmutableArray<TBase> immutableArray, IEnumerable<TDerived> items, IEqualityComparer<TBase>? comparer)
            {
                Requires.NotNull(items, nameof(items));

                comparer ??= EqualityComparer<TBase>.Default;

                int i = 0;
                int n = immutableArray.Length;
                foreach (TDerived item in items)
                {
                    if (i == n)
                    {
                        return false;
                    }

                    if (!comparer.Equals(immutableArray[i], item))
                    {
                        return false;
                    }

                    i++;
                }

                return i == n;