EnumerableExtensions
using System.Collections.Generic;
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 IEnumerable<TResult> SelectButLast<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector)
{
using (IEnumerator<TSource> iterator = source.GetEnumerator()) {
bool flag = true;
TSource arg = default(TSource);
while (iterator.MoveNext()) {
if (!flag)
yield return selector(arg);
arg = iterator.Current;
flag = false;
}
}
}
}
}