Stashbox by Peter Csajtai

<PackageReference Include="Stashbox" Version="3.3.0" />

 EnumerableExtensions

static class EnumerableExtensions
using Stashbox.Utils; using Stashbox.Utils.Data; using System.Collections.Generic; using System.Linq.Expressions; namespace System.Linq { internal static class EnumerableExtensions { public static TEnumerable[] CastToArray<TEnumerable>(this IEnumerable<TEnumerable> enumerable) { TEnumerable[] array = enumerable as TEnumerable[]; if (array == null) return enumerable.ToArray(); return array; } public static TEnumerable[] WhereOrDefault<TEnumerable>(this IEnumerable<TEnumerable> enumerable, Func<TEnumerable, bool> predicate) { TEnumerable[] array = enumerable.Where(predicate).CastToArray(); if (array.Length == 0) return null; return array; } public static int GetReferenceIndex<TElement>(this TElement[] array, TElement element) where TElement : class { if (array == null || array.Length == 0) return -1; int num = array.Length; if (num == 1) { if (array[0] != element) return -1; return 0; } for (int i = 0; i < num; i++) { if (array[i] == element) return i; } return -1; } public static bool ContainsReference<TElement>(this TElement[] array, TElement element) where TElement : class { return array.GetReferenceIndex(element) != -1; } public static TResult[] SelectButLast<TSource, TResult>(this TSource[] source, Func<TSource, TResult> selector) { int num = source.Length; if (num == 1) return Constants.EmptyArray<TResult>(); int num2 = num - 1; TResult[] array = new TResult[num2]; for (int i = 0; i < num2; i++) { array[i] = selector(source[i]); } return array; } public static TResult LastElement<TResult>(this TResult[] source) { return source[source.Length - 1]; } public static ParameterExpression[] AsParameters(this Type[] source) { int num = source.Length; ParameterExpression[] array = new ParameterExpression[num]; for (int i = 0; i < num; i++) { array[i] = source[i].AsParameter(null); } return array; } public static Pair<bool, ParameterExpression>[] AsParameterPairs(this ParameterExpression[] source) { int num = source.Length; Pair<bool, ParameterExpression>[] array = new Pair<bool, ParameterExpression>[num]; for (int i = 0; i < num; i++) { array[i] = new Pair<bool, ParameterExpression>(false, source[i]); } return array; } public static ExpandableArray<TItem> AsExpandableArray<TItem>(this IEnumerable<TItem> enumerable) { return ExpandableArray<TItem>.FromEnumerable(enumerable); } public static Stashbox.Utils.Data.Stack<TItem> AsStack<TItem>(this IEnumerable<TItem> enumerable) { return Stashbox.Utils.Data.Stack<TItem>.FromEnumerable(enumerable); } } }