Skip to content

Commit

Permalink
IndexOf() stackOverflow fix (#20083) (#21965)
Browse files Browse the repository at this point in the history
* Update EnumerableExtensions.cs

* Update EnumerableExtensions.cs
  • Loading branch information
kubaflo committed Apr 22, 2024
1 parent 7ba723c commit 86e22dd
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Core/src/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,16 @@ public static int IndexOf<T>(this IEnumerable<T> enumerable, Func<T, bool> predi
throw new ArgumentNullException(nameof(enumerable));
}

if (enumerable is IList<T> list)
if (enumerable is List<T> list)
{
var listPredicate = new Predicate<T>(predicate);
return list.FindIndex(listPredicate);
}

if (enumerable is T[] array)
{
return list.IndexOf(predicate);
var arrayPredicate = new Predicate<T>(predicate);
return Array.FindIndex(array, arrayPredicate);
}

var i = 0;
Expand Down

0 comments on commit 86e22dd

Please sign in to comment.